30something GSOH seeks help with the basics of a minor programme
Hello,
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
Code:
/*
* 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)
{
/* DECLARE VARIABLES */
// 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
display_line(&line);
getch();
/* 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");
getch();
/* 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");
getch();
/*initialise "size"
size=0;
/* Size Selection Function */
printf("please la de dah... ");
scanf("%d",&size);
/* 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);
/*
//Triangle:
line(a,b,c,d);
line(c,d,e,f);
line(e,f,a,b);
*/
/* 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;
}
http://www.cs.ccsu.edu/~markov/ccsu_courses/161Syllabus.html#CS161%20-%20C%20programming,%20Lecture%209
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=newelement();
(*q).val=val; /* alternative notation: q->val=val
(*q).next=NULL;
if (p==NULL) /* the list is empty
p=q;
else { /* the list is not empty
t=p;
while ((*t).next!=NULL) /* move t at the and of the list
t=(*t).next;
(*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
scanf("%d",&x);
if (x>0) append(x); /* push(x) stores the list in reverse order
} while (x>0); /* x<=0 end of input
q=p;
while (q!=NULL) { /* print the list
printf("%d\n",(*q).val);
q=(*q).next;
}
}
*/
/*
// p=point; np=new point ?
scanf("%d",°ree);
sin_value = sin (degree*PI/180);
cos_value = cos (degree*PI/180);
np1.x=(((p1.x*cos_value)-(p1.y*sin_value)));
np1.y=(((p1.y*cos_value)+(p1.x*sin_value)));
np2.x=(((p2.x*cos_value)-(p2.y*sin_value)));
np2.y=(((p2.y*cos_value)+(p2.x*sin_value)));
np3.x=(((p3.x*cos_value)-(p3.y*sin_value)));
np3.y=(((p3.y*cos_value)+(p3.x*sin_value)));
told(p1,p2,p3,0);
tnew(np1,np2,np3,3,1);
copyvalue();
or
new_x = cos(theta) * x - sin(theta) * y;
new_y = sin(theta) * x + cos(theta) * y;
or
x would equal (x * cos(Ang)) - (y * sin(Ang))
y would equal (y * cos(Ang)) + (x * sin(Ang))
*/
getch();
closegraph();
return 0;
}