'Graphical intersection of lines in C
I have a homework.
The homework is: There are 3 lines, theirs end with squares. Firstly The program has to view a circle is the lines are cut each other. (In other way: Take the intersection of the 3 lines). Secondly, the program has to change the background, along the lines. Each line's both side define the background with a colour. And how rotating the lines, together with them, the background colour changing. There are 3 lines, and 6 background colour. The border of background color is along the lines.
The programming enviroment is the DevC++ (we must use the c++ console applicaton, but in the lesson we not coding in c++, just c...)
Youtube video about the exercise/homework
I've tried implement of the lines' intersection, but It doesn't work very well. And I don't have any idea, how can I implement of the colourful background change.
What kind of knowledge is needed for it?
I would like to if somebody can suggest to me some: algorithm, webpage, tutorial, sourecode, anything what can help me. Or what is the name of my homework in english ( to google search) Cause I don't think, my solution is the best way to prepare my homework (maybe it won't succes)
Here the code, that I have did up to now (but it not perfect. The intersection of lines is not perfectly. It's not a beautiful solution, sorry I am not an expert C programmer):
- PONT = point, dot
- PONTH = aggregation of points
- atir = rewrite
- metszilleszt = fitting of intersection
- szakasz = section, phase... (there is too many in english-hungarian) or platoon :-D
- eger = mouse
- egérkezelés = mouse control
balgomb = left button of mouse
# include "graphics.h" # include <conio.h> #include <stdio.h> typedef struct { float x1,x2,x3; } PONTH; typedef struct { double x,y; }PONT; PONTH atir(PONT A){ PONTH C; C.x1=A.x; C.x2=A.y; C.x3=1; return C; } PONTH metszilleszt(PONTH A,PONTH B){ PONTH C; C.x1=(A.x2*B.x3)-(A.x3*B.x2); C.x2=-(A.x1*B.x3)+(A.x3*B.x1); C.x3=(A.x1*B.x2)-(A.x2*B.x1); return C; } int main() { //PONT szakasz[4]={100,50,300,200,30,130,140,170}; PONT szakasz[6]={100,50,300,200,30,130,140,170,30,70,210,40}; int ap; int gd,gm; int page =0; gd=VGA;gm=VGAMED; initgraph(&gd,&gm,""); PONTH A,B,C,D,E,F; PONTH tmp1,tmp2,tmp3,tmp4,tmp5,tmp6; PONT pont; for(;;){ setactivepage(page); cleardevice(); A=atir(szakasz[0]); B=atir(szakasz[1]); C=atir(szakasz[2]); D=atir(szakasz[3]); E=atir(szakasz[4]); F=atir(szakasz[5]); tmp1=metszilleszt(A,B); tmp2=metszilleszt(C,D); tmp3=metszilleszt(E,F); tmp4=metszilleszt(tmp2,tmp1); tmp5=metszilleszt(tmp3,tmp1); tmp6=metszilleszt(tmp3,tmp2); pont.x=int (tmp3.x1/tmp3.x3); pont.y=int (tmp3.x2/tmp3.x3); //printf("%f %f\n",pont.x,pont.y); // good if((((tmp4.x2/tmp4.x3)>=szakasz[0].y) && ((tmp4.x2/tmp4.x3)<=szakasz[1].y)) && (((tmp4.x1/tmp4.x3)>=szakasz[0].x) && ((tmp4.x1/tmp4.x3)<=szakasz[1].x)) || (((tmp4.x2/tmp4.x3)>=szakasz[0].y) && ((tmp4.x2/tmp4.x3)<=szakasz[1].y)) && (((tmp4.x1/tmp4.x3)<=szakasz[0].x) && ((tmp4.x1/tmp4.x3)>=szakasz[1].x))) { setcolor(RED); fillellipse(int (tmp4.x1/tmp4.x3),int (tmp4.x2/tmp4.x3),5,5); } if((((tmp5.x2/tmp5.x3)>=szakasz[0].y) && ((tmp5.x2/tmp5.x3)<=szakasz[1].y)) && (((tmp5.x1/tmp5.x3)>=szakasz[0].x) && ((tmp5.x1/tmp5.x3)<=szakasz[1].x)) || (((tmp5.x2/tmp5.x3)>=szakasz[0].y) && ((tmp5.x2/tmp5.x3)<=szakasz[1].y)) && (((tmp5.x1/tmp5.x3)<=szakasz[0].x) && ((tmp5.x1/tmp5.x3)>=szakasz[1].x))) { setcolor(RED); //fillellipse(int (tmp5.x1/tmp5.x3),int (tmp5.x2/tmp5.x3),5,5); fillellipse(int (tmp5.x1/tmp5.x3),int (tmp5.x2/tmp5.x3),5,5); } if((((tmp6.x2/tmp6.x3)>=szakasz[0].y) && ((tmp6.x2/tmp6.x3)<=szakasz[1].y)) && (((tmp6.x1/tmp6.x3)>=szakasz[0].x) && ((tmp6.x1/tmp6.x3)<=szakasz[1].x)) || (((tmp6.x2/tmp6.x3)>=szakasz[0].y) && ((tmp6.x2/tmp6.x3)<=szakasz[1].y)) && (((tmp6.x1/tmp6.x3)<=szakasz[0].x) && ((tmp6.x1/tmp6.x3)>=szakasz[1].x))) { setcolor(RED); fillellipse(int (tmp6.x1/tmp6.x3),int (tmp6.x2/tmp6.x3),5,5); } //else{ setcolor(RED); // fillellipse(int (tmp3.x1/tmp3.x3),int (tmp3.x2/tmp3.x3),5,5); } /* Egerkezeles */ if (!balgomb) ap = getactivepoint((pont2d*)szakasz,6,6); if (ap >= 0 && balgomb) { szakasz[ap].x = egerx; szakasz[ap].y = egery; } /* Egerkezeles vege */ setcolor(WHITE); line((int)szakasz[0].x,(int)szakasz[0].y,(int)szakasz[1].x,(int)szakasz[1].y); rectangle((int)szakasz[0].x,(int)szakasz[0].y, (int)szakasz[0].x+4, (int)szakasz[0].y+4); rectangle((int)szakasz[1].x,(int)szakasz[1].y, (int)szakasz[1].x+4, (int)szakasz[1].y+4); line((int)szakasz[2].x,(int)szakasz[2].y,(int)szakasz[3].x,(int)szakasz[3].y); rectangle((int)szakasz[2].x,(int)szakasz[2].y, (int)szakasz[2].x+4, (int)szakasz[2].y+4); rectangle((int)szakasz[3].x,(int)szakasz[3].y, (int)szakasz[3].x+4, (int)szakasz[3].y+4); line((int)szakasz[4].x,(int)szakasz[4].y,(int)szakasz[5].x,(int)szakasz[5].y); rectangle((int)szakasz[4].x,(int)szakasz[4].y, (int)szakasz[4].x+4, (int)szakasz[4].y+4); rectangle((int)szakasz[5].x,(int)szakasz[5].y, (int)szakasz[5].x+4, (int)szakasz[5].y+4); setvisualpage(page); page = 1-page; if (kbhit()) break; } getch(); closegraph(); return(0); }
Solution 1:[1]
Regarding the intersections of lines, you should read the article of Loren Shure and Lucio Cetto from Matworks on Loren's blog:
It's in Matlab but the principles are the same.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Clement J. |
