Skip to content

Commit 7366a19

Browse files
authored
Update line.c
1 parent 0a03ee6 commit 7366a19

File tree

1 file changed

+39
-30
lines changed

1 file changed

+39
-30
lines changed

Line Drawing/line.c

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,29 @@ int main(int argc, char *argv[]) {
2222
SDL_Event event; //used for reading events in the event queue
2323
Line point; //this line object will be manipulated by adjusting its end-point coordinates, and rendered into the renderer
2424

25-
int xPixels = (WIDTH / UNIT); //defines the number of horizontal pixels to be added to/subtracted from the current x-coordinate of a point
26-
int yPixels = (WIDTH / UNIT); //defines the number of vertical pixels to be added to/subtracted from the current y-coordinate of a point
27-
2825
//mark the center point
2926
int center[2] = {WIDTH / 2, HEIGHT / 2};
3027

31-
//mark the beginning of the animation
32-
//this is probably one of the few instances where goto is advantageous
33-
BEGIN:
28+
int xPixels = (WIDTH - MARGIN) / UNIT; //defines the number of horizontal pixels to be added to/subtracted from the current x-coordinate of a point
29+
int yPixels = (HEIGHT - MARGIN) / UNIT; //defines the number of vertical pixels to be added to/subtracted from the current y-coordinate of a point
30+
31+
if(xPixels < yPixels) {
32+
yPixels = xPixels;
33+
xPixels = yPixels;
34+
}
35+
else {
36+
xPixels = yPixels;
37+
yPixels = xPixels;
38+
}
3439

3540
//set the background color to {199, 0, 57, 255}
3641
SDL_SetRenderDrawColor(renderer, 199, 0, 57, 255);
3742
SDL_RenderClear(renderer);
3843

44+
//mark the beginning of the animation
45+
//this is probably one of the few scenarios where goto is advantageous
46+
BEGIN:
47+
3948
//initially set the drawing color to white
4049
SDL_RenderPresent(renderer);
4150
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
@@ -44,13 +53,13 @@ int main(int argc, char *argv[]) {
4453
while(1) {
4554
//set the initial position of the line to vertical, centered
4655
point.A[0] = center[0];
47-
point.A[1] = xPixels;
56+
point.A[1] = HEIGHT - (yPixels * UNIT);
4857

4958
point.B[0] = center[0];
5059
point.B[1] = center[1];
5160

5261
//FOR QUADRANT 1:
53-
while(point.A[1] + yPixels <= point.B[1]) {
62+
while(point.A[1] < center[1]) {
5463
//check if user wants to restart the animation from the beginning or terminate the program
5564
switch(userRequest(&event)) {
5665
case RESTART:
@@ -59,18 +68,18 @@ int main(int argc, char *argv[]) {
5968
goto END;
6069
}
6170

62-
//Change the position of the line accordingly
63-
point.A[1] += yPixels;
64-
point.B[0] += xPixels;
65-
6671
//display the line in its updated position
6772
drawLine(&renderer, point.A, point.B);
6873
SDL_RenderPresent(renderer);
69-
SDL_Delay(100);
74+
SDL_Delay(DELAY);
75+
76+
//Change the position of the line accordingly
77+
point.A[1] += yPixels;
78+
point.B[0] += xPixels;
7079
}
7180

7281
//FOR QUADRANT 4:
73-
while(point.B[0] - xPixels > center[0]) {
82+
while(point.B[0] > center[0]) {
7483
//check if user wants to restart the animation from the beginning or terminate the program
7584
switch(userRequest(&event)) {
7685
case RESTART:
@@ -79,18 +88,18 @@ int main(int argc, char *argv[]) {
7988
goto END;
8089
}
8190

82-
//Change the position of the line accordingly
83-
point.B[0] -= xPixels;
84-
point.A[1] += yPixels;
85-
8691
//display the line in its updated position
8792
drawLine(&renderer, point.A, point.B);
8893
SDL_RenderPresent(renderer);
89-
SDL_Delay(100);
94+
SDL_Delay(DELAY);
95+
96+
//Change the position of the line accordingly
97+
point.B[0] -= xPixels;
98+
point.A[1] += yPixels;
9099
}
91100

92101
//FOR QUADRANT 3:
93-
while(point.A[1] - yPixels > center[1]) {
102+
while(point.A[1] - 5 > center[1]) {
94103
//check if user wants to restart the animation from the beginning or terminate the program
95104
switch(userRequest(&event)) {
96105
case RESTART:
@@ -99,14 +108,14 @@ int main(int argc, char *argv[]) {
99108
goto END;
100109
}
101110

102-
//Change the position of the line accordingly
103-
point.B[0] -= xPixels;
104-
point.A[1] -= yPixels;
105-
106111
//display the line in its updated position
107112
drawLine(&renderer, point.A, point.B);
108113
SDL_RenderPresent(renderer);
109-
SDL_Delay(100);
114+
SDL_Delay(DELAY);
115+
116+
//Change the position of the line accordingly
117+
point.B[0] -= xPixels;
118+
point.A[1] -= yPixels;
110119
}
111120

112121
//FOR QUADRANT 2:
@@ -119,14 +128,14 @@ int main(int argc, char *argv[]) {
119128
goto END;
120129
}
121130

122-
//Change the position of the line accordingly
123-
point.B[0] += xPixels;
124-
point.A[1] -= yPixels;
125-
126131
//display the line in its updated position
127132
drawLine(&renderer, point.A, point.B);
128133
SDL_RenderPresent(renderer);
129-
SDL_Delay(100);
134+
SDL_Delay(DELAY);
135+
136+
//Change the position of the line accordingly
137+
point.B[0] += xPixels;
138+
point.A[1] -= yPixels;
130139
}
131140

132141
//set the drawing color to a randomly generated color

0 commit comments

Comments
 (0)