30something GSOH seeks help with the basics of a minor programme

This is a discussion on 30something GSOH seeks help with the basics of a minor programme within the C Programming forums, part of the General Programming Boards category; Hello, I'm almost completely useless at C, and as part of a project I've got to conjure up a programme, ...

  1. #1
    Registered User
    Join Date
    May 2007

    Unhappy 30something GSOH seeks help with the basics of a minor programme


    I'm almost completely useless at C, and as part of a project I've got to conjure up a programme, and I'm just hoping someone can give me push to get it started.

    I want to make a programme where the user can draw a triangle (and eventually any polygon) and set the size, the centre point, and rotate the thing freely.
    I have to use linked lists in the programme to let the user draw multiple shapes.

    I almost understand how to do this, but not quite. I'll show you what I've got so far.
    (as you may rapidly notice, I've only coded the first half, the rest is bits of example code that I don't quite understand how to use yet. I accidentally deleted what I had coded before - not that it was any good)

    please be merciful!

    I'm just asking for a bit of help to get started

     * Shapeshifter
     * Passing by reference
    typedef struct long_line_type //this is my structure "long_line_type" and contains the following bits of info:
        int start_x, start_y;
        int end_x, end_y;
        int colour;
    } line_type; // this is a nickname for the structure "long_line_type"
    typedef struct {int x, y, z;} Point;    // exclude z for 2D
    // a Triangle is given by three points: Point V0, V1, V2
    typedef struct {
        int        a, b, c;                // Vertex indices
        char    col;                    // Colour
        float    cenZ;                    // Z centroid (used for sorting)
    } Triangle;
     * Display a single line
    #include "stdio.h"
    #include "graphics_lib.h"
    #include "math.h"
    #include "conio.h"
    //First define "this_line" as a pointer.
    void display_line(line_type *this_line) //"display_line" = "draw_line"
     /* Open a graphics window */
     initwindow(800, 600);
     /* Set the colour */
     setcolor(this_line->colour); //to set colour of line. the arrow dereferernces the *
     /* Draw the line */
     line(this_line->start_x,this_line->start_y,this_line->end_x,this_line->end_y); //to set a co-ord to draw line
    //line is a pointer to a line type
    void get_line(line_type *line_pointer)
     //accessing the members of the "structure" (a collection of differnet variables like it's 1.2,1.2,1.3 etc but with words
    //"line" is the actual name of the varaible
     //user the (). notation when you haven't got a pointer variable; use the arrow notation when you have got a pointer variable
    scanf("%d", &line_pointer->start_x);
    scanf("%d", &line_pointer->start_y);
    scanf("%d", &line_pointer->end_x);
    scanf("%d", &line_pointer->end_y);
    scanf("%d", &line_pointer->colour);
    //can't return anything
    //you have stored the address &line in the pointer called "line_pointer"; line_pointer contains all the address of the 5 scanf bits above.
    //line_pointer is a "postcode" (pointer) that sends data to the "house" (structure) called "line"
    //passing by reference
     * THE MAIN FUNCTION - the program starts executing here
    int main(void)
    //	char key_press;
    	int x_position;
    	int y_position;
    	int size;
    //	int shape;
    	int a;
    	int b;
    	int c;
    	int d;
    	int e;
    	int f;
     //declare a variable of line_type
     line_type line; //I've now defined summat called "line" that is now a structure of the template called "line_type"
     //call the get_line function
     get_line(&line); //send the function called "get_line" the address of "line".
     //some printfs
     printf("The line goes from %d to %d, and from %d to %d; it's colour is code %d \n",line.start_x, line.start_y, line.end_x, line.end_y, line.colour);
     /*void function{}
     /* Get line information from the user */
     /* Display the lines */
        //calling the function: "line" is a local variable; "display_line" is the type of variable to be called
     /* Title Screen Function */
    		/* Open a graphics window */
    		/* Make it 800 pixels wide by 600 pixels high */
        initwindow(800, 600);
    	printf("SHAPESHIFTER, a shape drawing programme");
    	printf("Press space bar to continue");
    	/* Instruction Screen Function */
    	printf("1. blah blah blah; 2. blah blah blah; 3. blah blah blah; 4. blah blah blah; 5. blah blah blah; 6. blah blah blah");
     	printf("Press space bar to continue");
    	/*initialise "size"
    	/* Size Selection Function */
    			printf("please la de dah... ");
    	/* Easel Screen Function */
    	/* Shape Selection Switch Function */
    			//Getting centre point co-ordinates
    			printf("please choose a value for the x and y co-ordinates:\n");
    			scanf("%d %d", &x_position, &y_position);
    	        a = x_position;
    		    b = y_position+(size/2);
    			c = x_position-(size/2);
    		    d = y_position-(size/2);
    			e = x_position+(size/2);
    		    f = y_position-(size/2);
    			/* LINKED LIST EXAMPLE http://www.c.happycodings.com/Data_Structures/code5.html
    #include <stdlib.h>   <<---- this too
    #include <string.h>   <<---- this too
    struct llist {
     char *str;
     struct llist *next;  //a pointer to the next element
    int main(void) { <<----- watch it!!!
     char line[1024];
     struct llist *head = NULL;  //a pointer to the first element in the list
     struct llist *new = NULL;
     while(fgets(line, 1024, stdin) != NULL) {
      new = (struct llist *)malloc(sizeof(struct llist));
      new->next = head;
      head = new;
      new->str = strdup(line);
     while(head != NULL) {
      printf("%s\n", head->str);
      head = head->next;
      2. Allocating storage for list elements
    struct list *newelement() { //creates an instance of the list structure
       return (struct list *)malloc(sizeof(struct list));
    3. Adding list elements
    void push(int val) { /* add an element in the beginning of the list
      struct list *q;
      q=newelement();    /* create new element
      (*q).val=val;      /* assign the value member 
      (*q).next=p;       /* set the pointer to the next element
      p=q;               /* set the global list pointer 
    void append(int val) { /* add an element at the end of the list 
      struct list *q, *t;
      (*q).val=val;   /* alternative notation: q->val=val 
      if (p==NULL)    /* the list is empty 
      else {          /* the list is not empty 
        while ((*t).next!=NULL)  /* move t at the and of the list 
        (*t).next=q;             /* connect the new element 
    4. Reading and printing lists
    void main() {
      struct list *q;
      int x;
      p=NULL;   /* initialize the list - empty list 
      do {      /* read numbers and store them in a list 
        if (x>0) append(x); /* push(x) stores the list in reverse order 
      } while (x>0);  /* x<=0 end of input 
      while (q!=NULL) {  /* print the list 
    // p=point; np=new point ?
    			sin_value = sin (degree*PI/180);
    			cos_value = cos (degree*PI/180);
    	new_x = cos(theta) * x - sin(theta) * y;
        new_y = sin(theta) * x + cos(theta) * y;
    	x would equal (x * cos(Ang)) - (y * sin(Ang))
    	y would equal (y * cos(Ang)) + (x * sin(Ang))
     return 0;

  2. #2
    Registered User
    Join Date
    Sep 2006
    I don't understand just what you want to do with the linked list? And why do you need to sort it? Are you supposed to pre-compute several triangle's data and then display them by going through the linked list, maybe?

    Looks like you're using a Borland compiler, are you going to run this in a console window of Windows?

    I haven't run your program yet, but are you able to draw your triangle OK, to the specifications you need (size, shape, type, whatever)?

    Do you need special color's or line types, etc., with this?

    Just fill us in on what your code is doing, and not doing, right now. Right or wrong - give us them deets! (details)

  3. #3
    Registered User
    Join Date
    May 2007
    Thanks Adak.

    I really appreciate your help.

    I have to make a vector graphics drawing programme that lets a user draw at least four kinds of shapes: a line, circle, triangle, and square, but ideally any polygons.
    They have to be able to set the size, colour (and fill them in), orientation (rotate), and positioning, and the order they're drawn (the zed order).

    I've got to use a linked list to store the date for the data for the elements of the shape... I think that means the x and y co-ords, or radius for the circle.

    I thought this meant putting the elements in a structure and then putting the structures in linked lists, but I don't quite understand how it all works.

    I'm using MS Visual C++ 6.0, but I've got a problem with the paths on my laptop... so it won't actually compileat the moment! I ought to check whether i can make it work on another PC.

    It has to run on a standard MS Dos window.

    I'm only going to use the basic 16 colours - I planned to make all the selections like toggled switches, so for instance you'd press the left or right cursor to switch the name of the colour; pressing a key to select it.
    The same sort of thing for sizes (limited to integers).

    "...pre-compute several triangle's data and then display them by going through the linked list, maybe?"
    I think this is the idea, but not just triangles.

    I don't know whether the triangle draws yet...

    ..sorry I'm not able to give much; I've only just started it the other day and had a few setbacks (plus I'm not completely with it yet)

    thanks anyway

  4. #4
    Registered User
    Join Date
    May 2007

    graphics header file


    Here's Graphics_lib.h - a necessary part of the programme i think

    // FILE: winbgim.h
    // WINBGIM Version 3.4 -- Dec 21, 1999
    // modified to run under Borland C++ 5.02 for Windows OR under the
    // mingw32 g++ compiler for Windows. There are also some additions
    // for mouse control and RGB colors. Documentation on how to use these
    // functions is available in www.cs.colorado.edu/~main/bgi/docs/
    // Modification log by Michael Main:
    // --Version 2.1: Oct 17, 1998
    //   Some mouse additions to Konstantin's original code
    // --Version 2.2: November 1, 1998
    //   Modified getch so that it can get the arrows and other keypad keys.
    // --Version 2.3: November 17, 1998
    //   Fixed a bug in getpixel.
    // --Version 2.4: November 25, 1998
    //   Added functions getactivepage() and getvisualpage() to get the current
    //   page number of the active and visual pages. In this implementation, the
    //   MAX_PAGES is set to 16, but I have used only pages 0 and 1 myself.
    // --Version 3.1: June 17, 1999
    //   Mostly implemented by Mark Richardson:
    //   Implements getimage and putimage.
    //   Adds new support for rgb colors.
    // --Version 3.2: June 21, 1999
    //   Made modifications so that everything works with the mingw32
    //   G++ compiler for Windows. Details for installing and using this
    //   free compiler are in www.cs.colorado.edu/~main/mingw32/README.html
    // --Version 3.3: Oct 4, 1999
    //   Added ismouseclick and getmouseclick
    // --Version 3.4: Dec 21, 1999
    //   Added clearmouseclick.
    //   Fixed bug causing getmouseclick to fail when x and y are same variable.
    //   Fixed bug in setcolor that sometimes caused the fill color to change
    //   to the drawing color.
    #ifndef __GRAPHICS_H__
    #define __GRAPHICS_H__
    #define far
    #define huge
    #include <stddef.h>
    #include <conio.h>
    #include <windows.h>
    // added by PJK
    #ifndef __COLORS
    #define __COLORS
    enum colors {
    /** Some different definitions for the Mingw32 g++ compiler and
    *   the Borland 5.0 compiler. Added by Michael Main, June 21, 1999.
    *   Michael Main -- 10/17/98 */
    #if defined(_WINDOWS_H) || defined(_GNU_H_WINDOWS_H)
    /* MINGW32 G++ Compiler:
    *  Define the colors type in the same way that Borland does.
    *  Define CLR_INVALID from Borlands /win32/wingdi.h.
    *  Get the memset prototype from string.h. Note that sometimes <string.h> is
    *  actually <g++/String.h> for the Windows compiler because. In this case
    *  _STRING_H_ will not be defined but we can still pick it up from <../string.h>.
    *  Also define random for the bgidemo function.
    enum colors {
    #if !defined(CLR_INVALID)
    #include <string.h>
    #ifndef _STRING_H_
    #include <../string.h>
    /* BORLAND Compiler:
    * Colors are already defined in the BC5 conio.h, using COLORS.
    * So for Borland I'll replace this enum with a #define definition.
    //#define colors COLORS
    #ifndef random
    #define random(range) (rand() &#37; (range))
    // A definition to fix a misspelling of MAGENTA throughout this file:
    #define MAGENT MAGENTA
    enum write_modes {
    enum line_styles {
    enum fill_styles {
    enum text_directions {
    enum font_types {
    #define LEFT_TEXT             0
    #define CENTER_TEXT           1
    #define RIGHT_TEXT            2
    #define BOTTOM_TEXT           0
    #define TOP_TEXT              2
    #define NORM_WIDTH            1
    #define THICK_WIDTH           3
    #define DOTTEDLINE_LENGTH     2
    #define CENTRELINE_LENGTH     4
    #define USER_CHAR_SIZE        0
    #define MAXCOLORS             15
    #define CLIP_ON               1
    #define CLIP_OFF              0
    #define TOP_ON                1
    #define TOP_OFF               0
    // Definitions for the key pad extended keys are added here. I have also
    // modified getch() so that when one of these keys are pressed, getch will
    // return a zero followed by one of these values. This is the same way
    // that it works in conio for dos applications.
    // M. Main -- Nov 3, 1998
    #define KEY_HOME       71
    #define KEY_UP         72
    #define KEY_PGUP       73
    #define KEY_LEFT       75
    #define KEY_CENTER     76
    #define KEY_RIGHT      77
    #define KEY_END        79
    #define KEY_DOWN       80
    #define KEY_PGDN       81
    #define KEY_INSERT     82
    #define KEY_DELETE     83
    #define KEY_F1			  59
    #define KEY_F2         60
    #define KEY_F3         61
    #define KEY_F4			  62
    #define KEY_F5			  63
    #define KEY_F6			  64
    #define KEY_F7			  65
    #define KEY_F8			  66
    #define KEY_F9			  67
    enum graphics_errors {
       grOk = 0,
       grNoInitGraph = -1,
       grNotDetected = -2,
       grFileNotFound = -3,
       grInvalidDriver   = -4,
       grNoLoadMem = -5,
       grNoScanMem = -6,
       grNoFloodMem = -7,
       grFontNotFound = -8,
       grNoFontMem = -9,
       grInvalidMode =   -10,
       grError = -11,
       grIOerror = -12,
       grInvalidFont = -13,
       grInvalidFontNum = -14,
       grInvalidDeviceNum = -15,
       grInvalidVersion = -18
    /* Graphics drivers constants, includes X11 which is particular to XBGI. */
    #define DETECT                0
    #define CGA                1
    #define MCGA                  2
    #define EGA                3
    #define EGA64                 4
    #define EGAMONO                  5
    #define IBM8514                  6
    #define HERCMONO              7
    #define ATT400                8
    #define VGA                9
    #define PC3270                10
    /* Graphics modes constants. */
    #define CGAC0                 0
    #define CGAC1                 1
    #define CGAC2                 2
    #define CGAC3                 3
    #define CGAHI                 4
    #define MCGAC0                0
    #define MCGAC1                1
    #define MCGAC2                2
    #define MCGAC3                3
    #define MCGAMED                  4
    #define MCGAHI                5
    #define EGALO                 0
    #define EGAHI                 1
    #define EGA64LO                  0
    #define EGA64HI                  1
    #define EGAMONOHI             3
    #define HERCMONOHI               0
    #define ATT400C0              0
    #define ATT400C1              1
    #define ATT400C2              2
    #define ATT400C3              3
    #define ATT400MED             4
    #define ATT400HI              5
    #define VGALO                 0
    #define VGAMED                1
    #define VGAHI                 2
    #define VGAMAX                                          3
    #define PC3270HI              0
    #define IBM8514LO             0
    #define IBM8514HI             1
    typedef struct arccoordstype {
        int x;
        int y;
        int xstart;
        int ystart;
        int xend;
        int yend;
    } arccoordstype;
    typedef char fillpatterntype[8];
    typedef struct fillsettingstype {
        int pattern;
        int color;
    } fillsettingstype;
    typedef struct linesettingstype {
        int linestyle;
        unsigned int upattern;
        int thickness;
    } linesettingstype;
    typedef struct palettetype {
        unsigned char size;
        signed char colors[16];
    } palettetype;
    typedef struct textsettingstype {
        int font;
        int direction;
        int charsize;
        int horiz;
        int vert;
    } textsettingstype;
    typedef struct viewporttype {
        int left;
        int top;
        int right;
        int bottom;
        int clip;
    } viewporttype;
     // This struct was moved here to allow access to the struct (Mark Richardson 11/29/98)
    struct BGIimage {
    	short width;		// 2 bytes
       short height;		// 2 bytes  Note:This means bits is also aligned to 32bit(DWORD) boundry
    	char  bits[1];
    #define PROTO(ARGS) ARGS
    #define PROTO(ARGS) ()
    #if defined(__cplusplus)
    extern "C" {
    // Setting this variable to 0 increase speed of drawing but
    // correct redraw is not possible. By default this variable is initialized by 1
    extern int bgiemu_handle_redraw;
    // Default mode choosed by WinBGI if DETECT value is specified for
    // device parameter of initgraoh(). Default value is VGAMAX which
    // cause creation of maximized window (resolution depends on display mode)
    extern int bgiemu_default_mode;
    void _graphfreemem PROTO((void *ptr, unsigned int size));
    void* _graphgetmem PROTO((unsigned int size));
    void arc PROTO((int, int, int, int, int));
    void bar PROTO((int, int, int, int));
    void bar3d PROTO((int, int, int, int, int, int));
    void circle PROTO((int, int, int));
    void cleardevice PROTO((void));
    void clearviewport PROTO((void));
    void closegraph PROTO((void));
    void detectgraph PROTO((int *, int *));
    void drawpoly PROTO((int, int *));
    void ellipse PROTO((int, int, int, int, int, int));
    void fillellipse PROTO((int, int, int, int));
    void fillpoly PROTO((int, int *));
    void floodfill PROTO((int, int, int));
    void getarccoords PROTO((arccoordstype *));
    void getaspectratio PROTO((int *, int *));
    int getbkcolor PROTO((void));
    int getcolor PROTO((void));
    palettetype* getdefaultpalette PROTO((void));
    char* getdrivername PROTO((void));
    void getfillpattern PROTO((char const *));
    void getfillsettings PROTO((fillsettingstype *));
    int getgraphmode PROTO((void));
    void getimage PROTO((int, int, int, int, void *));
    void getlinesettings PROTO((linesettingstype *));
    int getmaxcolor PROTO((void));
    int getmaxmode PROTO((void));
    int getmaxx PROTO((void));
    int getmaxy PROTO((void));
    char* getmodename PROTO((int));
    void getmoderange PROTO((int, int *, int *));
    void getpalette PROTO((palettetype *));
    int getpalettesize PROTO((void));
    unsigned int getpixel PROTO((int, int));
    void gettextsettings PROTO((textsettingstype *));
    void getviewsettings PROTO((viewporttype *));
    int getx PROTO((void));
    int gety PROTO((void));
    void graphdefaults PROTO((void));
    char* grapherrormsg PROTO((int));
    int graphresult PROTO((void));
    unsigned int imagesize PROTO((int, int, int, int));
    void initgraph PROTO((int *, int *, char const *));
    int installuserdriver PROTO((char const *, int *));
    int installuserfont PROTO((char const *));
    void line PROTO((int, int, int, int));
    void linerel PROTO((int, int));
    void lineto PROTO((int, int));
    void moverel PROTO((int, int));
    void moveto PROTO((int, int));
    void outtext PROTO((char const *));
    void outtextxy PROTO((int, int, char const *));
    void pieslice PROTO((int, int, int, int, int));
    void putimage PROTO((int, int, void *, int));
    void putpixel PROTO((int, int, int));
    void rectangle PROTO((int, int, int, int));
    int registerbgidriver PROTO((void *));
    int registerbgifont PROTO((void *));
    void restorecrtmode PROTO((void));
    void sector PROTO((int, int, int, int, int, int));
    void setactivepage PROTO((int));
    void setallpalette PROTO((palettetype *));
    void setaspectratio PROTO((int, int));
    void setbkcolor PROTO((int));
    void setcolor PROTO((int));
    void setfillpattern PROTO((char const *, int));
    void setfillstyle PROTO((int, int));
    unsigned int setgraphbufsize PROTO((unsigned int));
    void setgraphmode PROTO((int));
    void setlinestyle PROTO((int, unsigned int, int));
    void setpalette PROTO((int, int));
    void setrgbpalette PROTO((int, int, int, int));
    void settextjustify PROTO((int, int));
    void settextstyle PROTO((int, int, int));
    void setusercharsize PROTO((int, int, int, int));
    void setviewport PROTO((int, int, int, int, int));
    void setvisualpage PROTO((int));
    void setwritemode PROTO((int));
    int textheight PROTO((char const *));
    int textwidth PROTO((char const *));
    //int getch PROTO((void));
    int kbhit PROTO((void));
    void delay PROTO((unsigned msec));
    void restorecrtmode PROTO((void));
    /* Additional functions for backwards compatibility */
    void graphics_on PROTO((void));
    void graphics_off PROTO((void));
    /* Prototypes for mouse handling functions. The mousex( ) and mousey( )
    *  functions return the most recent x and y coordinates detected from the
    *  mouse. For the other functions, the kind parameter should be one of these:
    *   WM_MOUSEMOVE       -- mouse movement
    *   WM_LBUTTONDBLCLK   -- left mouse button double-click
    *   WM_LBUTTONDOWN     -- left mouse button pushed down
    *   WM_LBUTTONUP       -- left mouse button released up
    *   WM_MBUTTONDBLCLK   -- middle mouse button double-click (might not work!)
    *   WM_MBUTTONDOWN     -- middle mouse button pushed down (might not work!)
    *   WM_MBUTTONUP       -- middle mouse button released up (might not work!)
    *   WM_RBUTTONDBLCLK   -- right mouse button double-click
    *   WM_RBUTTONDOWN     -- right mouse button pushed down
    *   WM_RBUTTONUP       -- right mouse button released up
    * The parameter h must be a void function with two integer parameters.
    * This function will be called whenever the corresponding event occurs.
    * The two integer parameters will be the x- and y-coordinates where the
    * event happened.
    * NOTE: The middle button events aren't being caught on my Windows 95 system.
    * I don't know why.
    * Added by Michael Main -- 11/3/98 and 10/4/99 and 12/21/99.
    int mousex PROTO(( ));
    int mousey PROTO(( ));
    void registermousehandler PROTO((UINT kind, void h(int, int)));
    //bool ismouseclick PROTO((UINT kind));
    //void getmouseclick PROTO((UINT kind, int& x, int& y));
    void clearmouseclick PROTO((UINT kind));
    /* Prototypes for other new functions, not in the original BGI graphics.
    *  There is also a new initwindow function that can be called instead of
    *  initgraph. The arguments are an explicit width and height.
    *  As of 11/3, the width is now the first parameter.
    *  The getactivepage() and getvisualpage() functions get the number of the
    *  current active or visual page.
    void initwindow PROTO((int, int));
    int getactivepage PROTO(( ));
    int getvisualpage PROTO(( ));
    /* Colors can be original bgi colors (ints in the range 0...MAXCOLORS) or
    *  RGB colors constructed from red, green and blue components between
    *  0 and 255.
    *    IS_BGI_COLOR(v): true if v is one of the original BGI colors
    *    IS_RGB_COLOR(v): true if v is one of the new RGB colors
    *    RED_VALUE(v) is the red value of an RGB color v
    *    GREEN_VALUE(v) is the red value of an RGB color v
    *    BLUE_VALUE(v) is the red value of an RGB color v
    *    COLOR(r,g,b): is the rgb color formed from a red, green and blue
    *                  value (all in the range 0...255).
    #define IS_BGI_COLOR(c) (((c) >= 0) && ((c) <= MAXCOLORS))
    #define IS_RGB_COLOR(c) ((c) & 0x04000000)
    #define RED_VALUE(v)   ((v) & 0xFF)
    #define GREEN_VALUE(v) (((v) >> 8) & 0xFF)
    #define BLUE_VALUE(v)  (((v) >> 16)& 0xFF)
    #define COLOR(r,g,b)   (0x04000000 | RGB(r,g,b))
    #if defined(__cplusplus)
    This should get rid of any errors...

    {@ @}
    Last edited by promsan; 05-13-2007 at 08:56 AM. Reason: whoops

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21