First partially-implemented functional release
[2dsolver.git] / src / display.c
1 #include <2dsolve.h>
2
3 #define SX(x, zoom) ((int) (((x) * (zoom)) + SCREEN_WIDTH / 2))
4 #define SY(y, zoom) ((int) (-((y) * (zoom)) + SCREEN_HEIGHT / 2))
5
6 int triangle_select;
7
8 /*
9 int
10 khandler (int key, struct display_info *disp, struct event_info *event)
11 {
12 if (event->state)
13 return;
14
15 if (event->code == SDLK_n)
16 triangle_select++;
17 else if (event->code == SDLK_p && triangle_select)
18 triangle_select--;
19 else
20 return 0;
21
22 display_geometry (disp, curr_info, curr_zoom);
23 display_refresh (disp);
24 }
25 */
26
27 /* Vamos a recurrir a isométricas:
28
29 Z
30 ^
31 |
32 |
33 |
34 |
35 |
36 |
37 _-O-_
38 _- -_
39 _- -_
40 _- -_
41 X < > Y
42
43 La idea será calcular las proyecciones de los tres ejes
44 sobre la pantalla, y multiplicar cada punto 3D por
45 estos ejes proyectados.
46
47 Las coordenadas 2D de estos ejes son:
48
49 X = (-cos (30), -sen (30)) = (-sqrt(3) / 2, -1/2)
50 Y = (cos (30), -sen (30)) = (sqrt(3) / 2, -1/2)
51 Z = (0, 1)
52 */
53
54
55 #define SQRT_3_2 0.86603
56
57 static void
58 line3d (display_t *disp, float x1, float y1, float z1,
59 float x2, float y2, float z2,
60 Uint32 color, float zoom)
61 {
62 float p1_x;
63 float p1_y;
64
65 float p2_x;
66 float p2_y;
67
68 p1_x = SQRT_3_2 * (y1 - x1);
69 p1_y = - 0.5 * (x1 + y1) + z1;
70
71 p2_x = SQRT_3_2 * (y2 - x2);
72 p2_y = - 0.5 * (x2 + y2) + z2;
73
74 line (disp,
75 SX (p1_x, zoom), SY (p1_y, zoom),
76 SX (p2_x, zoom), SY (p2_y, zoom),
77 color);
78 }
79
80 static void
81 mark3d (display_t *disp, float x1, float y1, float z1,
82 Uint32 color, float zoom)
83 {
84 float p1_x;
85 float p1_y;
86
87 p1_x = SQRT_3_2 * (y1 - x1);
88 p1_y = - 0.5 * (x1 + y1) + z1;
89
90 mark (disp,
91 SX (p1_x, zoom), SY (p1_y, zoom),
92 2, color, color);
93 }
94
95 void
96 display_solution (display_t *disp, struct problem2d *problem, float zoom)
97 {
98 int i, j;
99
100 float x, y, z;
101 float nx, ny, nz;
102
103 if (problem->solution == NULL)
104 {
105 disputs (disp, "Cannot paint solution: system not solved\n");
106 return;
107 }
108
109 line3d (disp, 0, 0, 0, 1, 0, 0, OPAQUE (0xffffff), zoom);
110 line3d (disp, 0, 0, 0, 0, 1, 0, OPAQUE (0xffffff), zoom);
111 line3d (disp, 0, 0, 0, 0, 0, 1, OPAQUE (0xffffff), zoom);
112
113 for (i = 0; i < problem->geometry->triangle_count; i++)
114 {
115 for (j = 0; j < TRIANGLE_NODES; j++)
116 {
117 x = problem->geometry->triangle_list[i]->nodes[j]->x;
118 y = problem->geometry->triangle_list[i]->nodes[j]->y;
119 z = 10.0 * gsl_vector_get (problem->solution, problem->geometry->triangle_list[i]->nodes[j]->index);
120
121 nx = problem->geometry->triangle_list[i]->nodes[(j + 1) % TRIANGLE_NODES]->x;
122 ny = problem->geometry->triangle_list[i]->nodes[(j + 1) % TRIANGLE_NODES]->y;
123 nz = 10.0 * gsl_vector_get (problem->solution, problem->geometry->triangle_list[i]->nodes[(j + 1) % TRIANGLE_NODES]->index);
124
125 line3d (disp, x, y, z, nx, ny, nz, OPAQUE (calc_color (400 * (z + nz))), zoom);
126 mark3d (disp, x, y, z, OPAQUE (calc_color (800 * z)), zoom);
127 // mark3d (disp, nx, ny, nz, OPAQUE (calc_color (8000 * nz)), zoom);
128 }
129 }
130 }
131
132 /*
133 void
134 display_geometry (display_t *disp, struct geometry_info *info, float zoom)
135 {
136 int i;
137 int select;
138
139 select = triangle_select % info->triangle_count;
140
141 clear (disp, OPAQUE (0));
142
143 display_printf (disp, 0, 0, OPAQUE (0xffa500), OPAQUE (0), "Triangle select: %5d/%d",
144 triangle_select, info->triangle_count);
145
146 for (i = 0; i < info->triangle_count; i++)
147 {
148 line (disp, SX (info->triangle_list[i]->nodes[0]->x, zoom),
149 SY (info->triangle_list[i]->nodes[0]->y, zoom),
150 SX (info->triangle_list[i]->nodes[1]->x, zoom),
151 SY (info->triangle_list[i]->nodes[1]->y, zoom),
152 OPAQUE (0xff0000));
153
154 line (disp, SX (info->triangle_list[i]->nodes[1]->x, zoom),
155 SY (info->triangle_list[i]->nodes[1]->y, zoom),
156 SX (info->triangle_list[i]->nodes[2]->x, zoom),
157 SY (info->triangle_list[i]->nodes[2]->y, zoom),
158 OPAQUE (0xff0000));
159
160 line (disp, SX (info->triangle_list[i]->nodes[2]->x, zoom),
161 SY (info->triangle_list[i]->nodes[2]->y, zoom),
162 SX (info->triangle_list[i]->nodes[0]->x, zoom),
163 SY (info->triangle_list[i]->nodes[0]->y, zoom),
164 OPAQUE (0xff0000));
165 }
166
167 line (disp, SX (FT_x (info->triangle_list[select]->nodes[0],
168 info->triangle_list[select]->nodes[1],
169 info->triangle_list[select]->nodes[2], 0.0, 0.0), zoom),
170 SY (FT_y (info->triangle_list[select]->nodes[0],
171 info->triangle_list[select]->nodes[1],
172 info->triangle_list[select]->nodes[2], 0.0, 0.0), zoom),
173 SX (FT_x (info->triangle_list[select]->nodes[0],
174 info->triangle_list[select]->nodes[1],
175 info->triangle_list[select]->nodes[2], 1.0, 0.0), zoom),
176 SY (FT_y (info->triangle_list[select]->nodes[0],
177 info->triangle_list[select]->nodes[1],
178 info->triangle_list[select]->nodes[2], 1.0, 0.0), zoom),
179 OPAQUE (0x00ffff));
180
181 line (disp, SX (FT_x (info->triangle_list[select]->nodes[0],
182 info->triangle_list[select]->nodes[1],
183 info->triangle_list[select]->nodes[2], 1.0, 0.0), zoom),
184 SY (FT_y (info->triangle_list[select]->nodes[0],
185 info->triangle_list[select]->nodes[1],
186 info->triangle_list[select]->nodes[2], 1.0, 0.0), zoom),
187 SX (FT_x (info->triangle_list[select]->nodes[0],
188 info->triangle_list[select]->nodes[1],
189 info->triangle_list[select]->nodes[2], 0.0, 1.0), zoom),
190 SY (FT_y (info->triangle_list[select]->nodes[0],
191 info->triangle_list[select]->nodes[1],
192 info->triangle_list[select]->nodes[2], 0.0, 1.0), zoom),
193 OPAQUE (0x00ffff));
194
195 line (disp, SX (FT_x (info->triangle_list[select]->nodes[0],
196 info->triangle_list[select]->nodes[1],
197 info->triangle_list[select]->nodes[2], 0.0, 1.0), zoom),
198 SY (FT_y (info->triangle_list[select]->nodes[0],
199 info->triangle_list[select]->nodes[1],
200 info->triangle_list[select]->nodes[2], 0.0, 1.0), zoom),
201 SX (FT_x (info->triangle_list[select]->nodes[0],
202 info->triangle_list[select]->nodes[1],
203 info->triangle_list[select]->nodes[2], 0.0, 0.0), zoom),
204 SY (FT_y (info->triangle_list[select]->nodes[0],
205 info->triangle_list[select]->nodes[1],
206 info->triangle_list[select]->nodes[2], 0.0, 0.0), zoom),
207 OPAQUE (0x00ffff));
208
209 for (i = 0; i < info->edge_count; i++)
210 line (disp, SX (info->edge_list[i]->p1->x, zoom),
211 SY (info->edge_list[i]->p1->y, zoom),
212 SX (info->edge_list[i]->p2->x, zoom),
213 SY (info->edge_list[i]->p2->y, zoom),
214 OPAQUE (0xffa500));
215
216 for (i = 0; i < info->point_count; i++)
217 mark (disp, SX (info->point_list[i]->x, zoom),
218 SY (info->point_list[i]->y, zoom),
219 2, OPAQUE (0x00ff00), 0);
220 }
221 */
222
223 void
224 prepare_display (display_t *disp)
225 {
226 /* display_register_key_handler (disp, SDLK_n, khandler);
227 display_register_key_handler (disp, SDLK_p, khandler);*/
228
229 /*
230 int i, j;
231
232 for (i = 0; i < A->size1; i++)
233 for (j = 0; j < A->size2; j++)
234 pset_abs (disp, i, j, OPAQUE (calc_color (15 * gsl_matrix_get (A, i, j))));
235 */
236 }
237
238