# Untitled

a guest
Jan 15th, 2020
66
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1.
2.
3. /*
4. Trasarea segmentelor de dreapta in spatiul discret
5. DDA line drawing algorithm
6. */
7.
8. #include <iostream>
9. #include <graphics.h>
10. #include <math.h>
11. struct punct
12. {
13. int x,y;
14. };
15.
16.
17. void BresGeneral(int, int, int, int);
18.
19. punct waitForLeftMouseClick2();
20.
21. int culoareCreion = GREEN;
22. int culoareHartie = WHITE;
23. void patr(int x,int y);
24. void waitForLeftMouseClick();
25.
26. void DDALine(int x1,int y1,int x2,int y2,int iColor);
27.
28. int x,y; // coordonate mouse
29. int latp=10; // marimea patratului
30. punct A,B;
31. int X,Y; // coordonate patrat
32. int Xa,Ya;
33. int main()
34. {
35. int x1,x2,y1,y2,iColor;
36.
37.
38.
39. x1 = 78;
40. y1 = 120;
41. x2 = 210;
42. y2 = 350;
43.
44. initwindow(800,800); //open a 400x300 graphics window
45. setcolor(GREEN);
46. for(int i=0;i<=50;i++) line(50,50+i*latp,550,50+i*latp);
47. for(int j=0;j<=50;j++) line(50+j*latp,50,50+j*latp,550);
48.
49. while(1){
50.
51. A=waitForLeftMouseClick2();
52. B=waitForLeftMouseClick2();
53. BresGeneral((A.x-50)/latp,(A.y-50)/latp,(B.x-50)/latp,(B.y-50)/latp);
54. }
55. //waitForLeftMouseClick();
56. return 0;
57. }
58. void patr(int i,int j){
59. // determin pozitia in grila
60. int xp,yp;
61. xp = 50 + i*latp;
62. yp = 50 + j*latp;
63.
64. // determin culoarea celulei
65. if(getpixel(xp+1,yp+1)==BLACK) setcolor(YELLOW);
66. else setcolor(BLACK);
67.
68. for(int k=1;k<latp;k++) line(xp+k,yp+1,xp+k,yp+latp);
69.
70. setcolor(GREEN);
71.
72. }
73.
74. void DDALine(int x1,int y1,int x2,int y2,int iColor)
75. {
76.
77. float dX,dY,iSteps;
78. float xInc,yInc,iCount,x,y;
79.
80. dX = x1 - x2;
81. dY = y1 - y2;
82.
83. if (fabs(dX) > fabs(dY))
84. iSteps = fabs(dX);
85. else
86. iSteps = fabs(dY);
87.
88. xInc = dX/iSteps;
89. yInc = dY/iSteps;
90.
91. x = x1;
92. y = y1;
93. circle((int)x,(int)y,1);
94.
95. for (iCount=1; iCount<=iSteps; iCount++)
96. {
97. putpixel((int)x,(int)y,iColor);
98. x -= xInc;
99. y -= yInc;
100. }
101. circle((int)x,(int)y,1);
102. return;
103. }
104.
105.
106. void waitForLeftMouseClick()
107. {
108. clearmouseclick(WM_LBUTTONDOWN);
109. const int DELAY = 50; // Milliseconds of delay between checks
110. int x, y;
111. while (!ismouseclick(WM_LBUTTONDOWN))
112. delay(DELAY);
113. getmouseclick(WM_LBUTTONDOWN, x, y);
114. }
115.
116. punct waitForLeftMouseClick2()
117. {
118. clearmouseclick(WM_LBUTTONDOWN);
119. const int DELAY = 50; // Milliseconds of delay between checks
120. punct p;
121. while (!ismouseclick(WM_LBUTTONDOWN))
122. delay(DELAY);
123. getmouseclick(WM_LBUTTONDOWN, p.x, p.y);
124. return p;
125. }
126.
127.
128.
129. // Bresenham generalizat
130. void BresGeneral(
131. int x1, int y1, int x2, int y2)
132. {
133.
134. int oct, dx,dy, absdx, absdy,c1,c2,t;
135. int culoare = culoareCreion;
136.
137. int x,y,i;
138. if(x1==x2) // vertical
139. {
140. if (y1>y2)
141. {
142. y=y1;
143. y1=y2;
144. y2=y;
145. }
146. for(y=y1; y<=y2; y++)
147. patr(x1,y);
148. return;
149. }
150.
151. if(y1==y2) // orizontal
152. {
153. if (x1>x2)
154. {
155. x=x1;
156. x1=x2;
157. x2=x;
158. }
159. for(x=x1; x<=x2; x++)
160. patr(x,y1);
161. return;
162. }
163.
164. dx=x2-x1;
165. dy=y2-y1;
166.
167. absdx=abs(dx);
168. absdy=abs(dy);
169. if(dx>0) // oct=1,2,7,8
170. {
171. if(dy>0) // oct 1,2
172. if(dx>=dy)
173. oct=1;
174. else
175. oct=2;
176. else if(dx>=absdy)
177. oct=8;
178. else
179. oct=7;
180. }
181. else // 3,4,5,6
182. {
183. if(dy>0) // oct 3,4
184. if(absdx>=dy)
185. oct=4;
186. else
187. oct=3;
188. else if(absdx>=absdy)
189. oct=5;
190. else
191. oct=6;
192. }
193.
194. if(absdy>absdx)
195. {
196. x=absdx;
197. absdx=absdy;
198. absdy=x;
199. }
200.
201. c1=absdy<<1;
202. c2=c1-(absdx<<1);
203. t=c1-absdx;
204. patr(x1,y1);
205.
206. for(i=1, x=x1, y=y1; i<absdx; i++)
207. {
208. if(t<0) // deplasament O
209. {
210. t+=c1;
211. switch(oct)
212. {
213. case 1:
214. case 8:
215. x++;
216. break;
217. case 4:
218. case 5:
219. x--;
220. break;
221. case 2:
222. case 3:
223. y++;
224. break;
225. case 6:
226. case 7:
227. y--;
228. break;
229. }
230. }
231. else
232. {
233. t+=c2; // deplasament D
234. switch(oct)
235. {
236. case 1:
237. case 2:
238. x++;
239. y++;
240. break;
241. case 3:
242. case 4:
243. x--;
244. y++;
245. break;
246. case 5:
247. case 6:
248. x--;
249. y--;
250. break;
251. case 7:
252. case 8:
253. x++;
254. y--;
255. break;
256. }
257. }
258. patr(x,y);
259. }
260. patr(x2,y2);
261. }
RAW Paste Data