]>
Commit | Line | Data |
---|---|---|
1fc25a89 JS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: misc.cpp | |
3 | // Purpose: Miscellaneous OGL support functions | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 12/07/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "misc.h" | |
14 | #endif | |
15 | ||
16 | // For compilers that support precompilation, includes "wx.h". | |
92a19c2e | 17 | #include "wx/wxprec.h" |
1fc25a89 JS |
18 | |
19 | #ifdef __BORLANDC__ | |
20 | #pragma hdrstop | |
21 | #endif | |
22 | ||
23 | #ifndef WX_PRECOMP | |
24 | #include <wx/wx.h> | |
25 | #endif | |
26 | ||
fd657b8a | 27 | #if wxUSE_DEPRECATED |
7c9955d1 | 28 | #include <wx/deprecated/wxexpr.h> |
fd657b8a | 29 | #endif |
1fc25a89 JS |
30 | |
31 | #include <wx/types.h> | |
32 | ||
3f1802b5 JS |
33 | #ifdef new |
34 | #undef new | |
35 | #endif | |
36 | ||
1fc25a89 JS |
37 | #include <ctype.h> |
38 | #include <math.h> | |
39 | #include <stdlib.h> | |
40 | ||
41 | #include <wx/ogl/basic.h> | |
42 | #include <wx/ogl/basicp.h> | |
43 | #include <wx/ogl/misc.h> | |
44 | #include <wx/ogl/constrnt.h> | |
45 | #include <wx/ogl/composit.h> | |
46 | ||
47 | wxFont* g_oglNormalFont; | |
48 | wxPen* g_oglBlackPen; | |
49 | wxPen* g_oglWhiteBackgroundPen; | |
50 | wxPen* g_oglTransparentPen; | |
51 | wxBrush* g_oglWhiteBackgroundBrush; | |
52 | wxPen* g_oglBlackForegroundPen; | |
53 | wxCursor* g_oglBullseyeCursor = NULL; | |
54 | ||
55 | char* oglBuffer = NULL; | |
56 | ||
57 | wxList oglObjectCopyMapping(wxKEY_INTEGER); | |
58 | ||
59 | ||
60 | ||
61 | void wxOGLInitialize() | |
62 | { | |
63 | g_oglBullseyeCursor = new wxCursor(wxCURSOR_BULLSEYE); | |
64 | ||
65 | g_oglNormalFont = new wxFont(10, wxSWISS, wxNORMAL, wxNORMAL); | |
66 | ||
67 | g_oglBlackPen = new wxPen("BLACK", 1, wxSOLID); | |
68 | ||
69 | g_oglWhiteBackgroundPen = new wxPen("WHITE", 1, wxSOLID); | |
70 | g_oglTransparentPen = new wxPen("WHITE", 1, wxTRANSPARENT); | |
71 | g_oglWhiteBackgroundBrush = new wxBrush("WHITE", wxSOLID); | |
72 | g_oglBlackForegroundPen = new wxPen("BLACK", 1, wxSOLID); | |
73 | ||
74 | OGLInitializeConstraintTypes(); | |
75 | ||
76 | // Initialize big buffer used when writing images | |
77 | oglBuffer = new char[3000]; | |
78 | ||
79 | } | |
80 | ||
81 | void wxOGLCleanUp() | |
82 | { | |
83 | if (oglBuffer) | |
84 | { | |
85 | delete[] oglBuffer; | |
86 | oglBuffer = NULL; | |
87 | } | |
88 | oglBuffer = NULL; | |
89 | if (g_oglBullseyeCursor) | |
90 | { | |
91 | delete g_oglBullseyeCursor; | |
92 | g_oglBullseyeCursor = NULL; | |
93 | } | |
94 | ||
95 | if (g_oglNormalFont) | |
96 | { | |
97 | delete g_oglNormalFont; | |
98 | g_oglNormalFont = NULL; | |
99 | } | |
100 | if (g_oglBlackPen) | |
101 | { | |
102 | delete g_oglBlackPen; | |
103 | g_oglBlackPen = NULL; | |
104 | } | |
105 | if (g_oglWhiteBackgroundPen) | |
106 | { | |
107 | delete g_oglWhiteBackgroundPen; | |
108 | g_oglWhiteBackgroundPen = NULL; | |
109 | } | |
110 | if (g_oglTransparentPen) | |
111 | { | |
112 | delete g_oglTransparentPen; | |
113 | g_oglTransparentPen = NULL; | |
114 | } | |
115 | if (g_oglWhiteBackgroundBrush) | |
116 | { | |
117 | delete g_oglWhiteBackgroundBrush; | |
118 | g_oglWhiteBackgroundBrush = NULL; | |
119 | } | |
120 | if (g_oglBlackForegroundPen) | |
121 | { | |
122 | delete g_oglBlackForegroundPen; | |
123 | g_oglBlackForegroundPen = NULL; | |
124 | } | |
125 | ||
126 | OGLCleanUpConstraintTypes(); | |
127 | } | |
128 | ||
129 | wxFont *oglMatchFont(int point_size) | |
130 | { | |
131 | wxFont *font = wxTheFontList->FindOrCreateFont(point_size, wxSWISS, wxNORMAL, wxNORMAL); | |
132 | #if 0 | |
133 | switch (point_size) | |
134 | { | |
135 | case 4: | |
136 | font = swiss_font_4; | |
137 | break; | |
138 | case 6: | |
139 | font = swiss_font_6; | |
140 | break; | |
141 | case 8: | |
142 | font = swiss_font_8; | |
143 | break; | |
144 | case 12: | |
145 | font = swiss_font_12; | |
146 | break; | |
147 | case 14: | |
148 | font = swiss_font_14; | |
149 | break; | |
150 | case 18: | |
151 | font = swiss_font_18; | |
152 | break; | |
153 | case 24: | |
154 | font = swiss_font_24; | |
155 | break; | |
156 | default: | |
157 | case 10: | |
158 | font = swiss_font_10; | |
159 | break; | |
160 | } | |
161 | #endif | |
162 | return font; | |
163 | } | |
164 | ||
165 | int FontSizeDialog(wxFrame *parent, int old_size) | |
166 | { | |
167 | if (old_size <= 0) | |
168 | old_size = 10; | |
169 | char buf[40]; | |
170 | sprintf(buf, "%d", old_size); | |
171 | wxString ans = wxGetTextFromUser("Enter point size", "Font size", buf, parent); | |
172 | if (ans == "") | |
173 | return 0; | |
174 | ||
175 | int new_size = atoi(ans); | |
176 | if ((new_size <= 0) || (new_size > 40)) | |
177 | { | |
178 | wxMessageBox("Invalid point size!", "Error", wxOK); | |
179 | return 0; | |
180 | } | |
181 | return new_size; | |
182 | /* | |
183 | char *strings[8]; | |
184 | strings[0] = "4"; | |
185 | strings[1] = "6"; | |
186 | strings[2] = "8"; | |
187 | strings[3] = "10"; | |
188 | strings[4] = "12"; | |
189 | strings[5] = "14"; | |
190 | strings[6] = "18"; | |
191 | strings[7] = "24"; | |
192 | char *ans = wxGetSingleChoice("Choose", "Choose a font size", 8, strings, parent); | |
193 | if (ans) | |
194 | { | |
195 | int size; | |
196 | sscanf(ans, "%d", &size); | |
197 | return oglMatchFont(size); | |
198 | } | |
199 | else return NULL; | |
200 | */ | |
201 | } | |
202 | ||
203 | // Centre a list of strings in the given box. xOffset and yOffset are the | |
204 | // the positions that these lines should be relative to, and this might be | |
205 | // the same as m_xpos, m_ypos, but might be zero if formatting from left-justifying. | |
206 | void oglCentreText(wxDC& dc, wxList *text_list, | |
207 | double m_xpos, double m_ypos, double width, double height, | |
208 | int formatMode) | |
209 | { | |
b9ac87bc | 210 | int n = text_list->GetCount(); |
1fc25a89 JS |
211 | |
212 | if (!text_list || (n == 0)) | |
213 | return; | |
214 | ||
215 | // First, get maximum dimensions of box enclosing text | |
216 | ||
217 | long char_height = 0; | |
218 | long max_width = 0; | |
219 | long current_width = 0; | |
220 | ||
221 | // Store text extents for speed | |
222 | double *widths = new double[n]; | |
223 | ||
b9ac87bc | 224 | wxNode *current = text_list->GetFirst(); |
1fc25a89 JS |
225 | int i = 0; |
226 | while (current) | |
227 | { | |
b9ac87bc | 228 | wxShapeTextLine *line = (wxShapeTextLine *)current->GetData(); |
1fc25a89 JS |
229 | dc.GetTextExtent(line->GetText(), ¤t_width, &char_height); |
230 | widths[i] = current_width; | |
231 | ||
232 | if (current_width > max_width) | |
233 | max_width = current_width; | |
fd657b8a | 234 | current = current->GetNext(); |
1fc25a89 JS |
235 | i ++; |
236 | } | |
237 | ||
238 | double max_height = n*char_height; | |
239 | ||
240 | double xoffset, yoffset, xOffset, yOffset; | |
241 | ||
242 | if (formatMode & FORMAT_CENTRE_VERT) | |
243 | { | |
244 | if (max_height < height) | |
245 | yoffset = (double)(m_ypos - (height/2.0) + (height - max_height)/2.0); | |
246 | else | |
247 | yoffset = (double)(m_ypos - (height/2.0)); | |
248 | yOffset = m_ypos; | |
249 | } | |
250 | else | |
251 | { | |
252 | yoffset = 0.0; | |
253 | yOffset = 0.0; | |
254 | } | |
255 | ||
256 | if (formatMode & FORMAT_CENTRE_HORIZ) | |
257 | { | |
258 | xoffset = (double)(m_xpos - width/2.0); | |
259 | xOffset = m_xpos; | |
260 | } | |
261 | else | |
262 | { | |
263 | xoffset = 0.0; | |
264 | xOffset = 0.0; | |
265 | } | |
266 | ||
b9ac87bc | 267 | current = text_list->GetFirst(); |
1fc25a89 JS |
268 | i = 0; |
269 | ||
270 | while (current) | |
271 | { | |
b9ac87bc | 272 | wxShapeTextLine *line = (wxShapeTextLine *)current->GetData(); |
1fc25a89 JS |
273 | |
274 | double x; | |
275 | if ((formatMode & FORMAT_CENTRE_HORIZ) && (widths[i] < width)) | |
276 | x = (double)((width - widths[i])/2.0 + xoffset); | |
277 | else | |
278 | x = xoffset; | |
279 | double y = (double)(i*char_height + yoffset); | |
280 | ||
281 | line->SetX( x - xOffset ); line->SetY( y - yOffset ); | |
fd657b8a | 282 | current = current->GetNext(); |
1fc25a89 JS |
283 | i ++; |
284 | } | |
285 | ||
286 | delete widths; | |
287 | } | |
288 | ||
289 | // Centre a list of strings in the given box | |
290 | void oglCentreTextNoClipping(wxDC& dc, wxList *text_list, | |
291 | double m_xpos, double m_ypos, double width, double height) | |
292 | { | |
b9ac87bc | 293 | int n = text_list->GetCount(); |
1fc25a89 JS |
294 | |
295 | if (!text_list || (n == 0)) | |
296 | return; | |
297 | ||
298 | // First, get maximum dimensions of box enclosing text | |
299 | ||
300 | long char_height = 0; | |
301 | long max_width = 0; | |
302 | long current_width = 0; | |
303 | ||
304 | // Store text extents for speed | |
305 | double *widths = new double[n]; | |
306 | ||
b9ac87bc | 307 | wxNode *current = text_list->GetFirst(); |
1fc25a89 JS |
308 | int i = 0; |
309 | while (current) | |
310 | { | |
b9ac87bc | 311 | wxShapeTextLine *line = (wxShapeTextLine *)current->GetData(); |
1fc25a89 JS |
312 | dc.GetTextExtent(line->GetText(), ¤t_width, &char_height); |
313 | widths[i] = current_width; | |
314 | ||
315 | if (current_width > max_width) | |
316 | max_width = current_width; | |
fd657b8a | 317 | current = current->GetNext(); |
1fc25a89 JS |
318 | i ++; |
319 | } | |
320 | ||
321 | double max_height = n*char_height; | |
322 | ||
323 | double yoffset = (double)(m_ypos - (height/2.0) + (height - max_height)/2.0); | |
324 | ||
325 | double xoffset = (double)(m_xpos - width/2.0); | |
326 | ||
b9ac87bc | 327 | current = text_list->GetFirst(); |
1fc25a89 JS |
328 | i = 0; |
329 | ||
330 | while (current) | |
331 | { | |
b9ac87bc | 332 | wxShapeTextLine *line = (wxShapeTextLine *)current->GetData(); |
1fc25a89 JS |
333 | |
334 | double x = (double)((width - widths[i])/2.0 + xoffset); | |
335 | double y = (double)(i*char_height + yoffset); | |
336 | ||
337 | line->SetX( x - m_xpos ); line->SetY( y - m_ypos ); | |
fd657b8a | 338 | current = current->GetNext(); |
1fc25a89 JS |
339 | i ++; |
340 | } | |
341 | delete widths; | |
342 | } | |
343 | ||
344 | void oglGetCentredTextExtent(wxDC& dc, wxList *text_list, | |
345 | double m_xpos, double m_ypos, double width, double height, | |
346 | double *actual_width, double *actual_height) | |
347 | { | |
b9ac87bc | 348 | int n = text_list->GetCount(); |
1fc25a89 JS |
349 | |
350 | if (!text_list || (n == 0)) | |
351 | { | |
352 | *actual_width = 0; | |
353 | *actual_height = 0; | |
354 | return; | |
355 | } | |
356 | ||
357 | // First, get maximum dimensions of box enclosing text | |
358 | ||
359 | long char_height = 0; | |
360 | long max_width = 0; | |
361 | long current_width = 0; | |
362 | ||
b9ac87bc | 363 | wxNode *current = text_list->GetFirst(); |
1fc25a89 JS |
364 | int i = 0; |
365 | while (current) | |
366 | { | |
b9ac87bc | 367 | wxShapeTextLine *line = (wxShapeTextLine *)current->GetData(); |
1fc25a89 JS |
368 | dc.GetTextExtent(line->GetText(), ¤t_width, &char_height); |
369 | ||
370 | if (current_width > max_width) | |
371 | max_width = current_width; | |
fd657b8a | 372 | current = current->GetNext(); |
1fc25a89 JS |
373 | i ++; |
374 | } | |
375 | ||
376 | *actual_height = n*char_height; | |
377 | *actual_width = max_width; | |
378 | } | |
379 | ||
380 | // Format a string to a list of strings that fit in the given box. | |
381 | // Interpret %n and 10 or 13 as a new line. | |
382 | wxStringList *oglFormatText(wxDC& dc, const wxString& text, double width, double height, int formatMode) | |
383 | { | |
384 | // First, parse the string into a list of words | |
385 | wxStringList word_list; | |
386 | ||
387 | // Make new lines into NULL strings at this point | |
388 | int i = 0; int j = 0; int len = strlen(text); | |
389 | char word[200]; word[0] = 0; | |
390 | bool end_word = FALSE; bool new_line = FALSE; | |
391 | while (i < len) | |
392 | { | |
393 | switch (text[i]) | |
394 | { | |
395 | case '%': | |
396 | { | |
397 | i ++; | |
398 | if (i == len) | |
399 | { word[j] = '%'; j ++; } | |
400 | else | |
401 | { | |
402 | if (text[i] == 'n') | |
403 | { new_line = TRUE; end_word = TRUE; i++; } | |
404 | else | |
405 | { word[j] = '%'; j ++; word[j] = text[i]; j ++; i ++; } | |
406 | } | |
407 | break; | |
408 | } | |
409 | case 10: | |
410 | { | |
411 | new_line = TRUE; end_word = TRUE; i++; | |
412 | break; | |
413 | } | |
414 | case 13: | |
415 | { | |
416 | new_line = TRUE; end_word = TRUE; i++; | |
417 | } | |
418 | case ' ': | |
419 | { | |
420 | end_word = TRUE; | |
421 | i ++; | |
422 | break; | |
423 | } | |
424 | default: | |
425 | { | |
426 | word[j] = text[i]; | |
427 | j ++; i ++; | |
428 | break; | |
429 | } | |
430 | } | |
431 | if (i == len) end_word = TRUE; | |
432 | if (end_word) | |
433 | { | |
434 | word[j] = 0; | |
435 | j = 0; | |
436 | word_list.Add(word); | |
437 | end_word = FALSE; | |
438 | } | |
439 | if (new_line) | |
440 | { | |
441 | word_list.Append(NULL); | |
442 | new_line = FALSE; | |
443 | } | |
444 | } | |
445 | // Now, make a list of strings which can fit in the box | |
446 | wxStringList *string_list = new wxStringList; | |
447 | ||
448 | char buffer[400]; | |
449 | buffer[0] = 0; | |
fd657b8a | 450 | wxStringListNode *node = word_list.GetFirst(); |
1fc25a89 JS |
451 | long x, y; |
452 | ||
453 | while (node) | |
454 | { | |
455 | wxString oldBuffer(buffer); | |
456 | ||
b9ac87bc | 457 | char *s = (char *)node->GetData(); |
1fc25a89 JS |
458 | if (!s) |
459 | { | |
460 | // FORCE NEW LINE | |
461 | if (strlen(buffer) > 0) | |
462 | string_list->Add(buffer); | |
463 | ||
464 | buffer[0] = 0; | |
465 | } | |
466 | else | |
467 | { | |
468 | if (buffer[0] != 0) | |
469 | strcat(buffer, " "); | |
470 | ||
471 | strcat(buffer, s); | |
472 | dc.GetTextExtent(buffer, &x, &y); | |
473 | ||
474 | // Don't fit within the bounding box if we're fitting shape to contents | |
475 | if ((x > width) && !(formatMode & FORMAT_SIZE_TO_CONTENTS)) | |
476 | { | |
477 | // Deal with first word being wider than box | |
478 | if (oldBuffer.Length() > 0) | |
479 | string_list->Add(oldBuffer); | |
480 | ||
481 | buffer[0] = 0; | |
482 | strcat(buffer, s); | |
483 | } | |
484 | } | |
485 | ||
fd657b8a | 486 | node = node->GetNext(); |
1fc25a89 JS |
487 | } |
488 | if (buffer[0] != 0) | |
489 | string_list->Add(buffer); | |
490 | ||
491 | return string_list; | |
492 | } | |
493 | ||
494 | void oglDrawFormattedText(wxDC& dc, wxList *text_list, | |
495 | double m_xpos, double m_ypos, double width, double height, | |
496 | int formatMode) | |
497 | { | |
498 | double xoffset, yoffset; | |
499 | if (formatMode & FORMAT_CENTRE_HORIZ) | |
500 | xoffset = m_xpos; | |
501 | else | |
502 | xoffset = (double)(m_xpos - (width / 2.0)); | |
503 | ||
504 | if (formatMode & FORMAT_CENTRE_VERT) | |
505 | yoffset = m_ypos; | |
506 | else | |
507 | yoffset = (double)(m_ypos - (height / 2.0)); | |
508 | ||
509 | dc.SetClippingRegion( | |
510 | (long)(m_xpos - width/2.0), (long)(m_ypos - height/2.0), | |
511 | (long)width, (long)height); | |
512 | ||
b9ac87bc | 513 | wxNode *current = text_list->GetFirst(); |
1fc25a89 JS |
514 | while (current) |
515 | { | |
b9ac87bc | 516 | wxShapeTextLine *line = (wxShapeTextLine *)current->GetData(); |
1fc25a89 JS |
517 | |
518 | dc.DrawText(line->GetText(), WXROUND(xoffset + line->GetX()), WXROUND(yoffset + line->GetY())); | |
fd657b8a | 519 | current = current->GetNext(); |
1fc25a89 JS |
520 | } |
521 | ||
522 | dc.DestroyClippingRegion(); | |
523 | } | |
524 | ||
525 | /* | |
526 | * Find centroid given list of points comprising polyline | |
527 | * | |
528 | */ | |
529 | ||
530 | void oglFindPolylineCentroid(wxList *points, double *x, double *y) | |
531 | { | |
532 | double xcount = 0; | |
533 | double ycount = 0; | |
534 | ||
b9ac87bc | 535 | wxNode *node = points->GetFirst(); |
1fc25a89 JS |
536 | while (node) |
537 | { | |
b9ac87bc | 538 | wxRealPoint *point = (wxRealPoint *)node->GetData(); |
1fc25a89 JS |
539 | xcount += point->x; |
540 | ycount += point->y; | |
fd657b8a | 541 | node = node->GetNext(); |
1fc25a89 JS |
542 | } |
543 | ||
b9ac87bc RD |
544 | *x = (xcount/points->GetCount()); |
545 | *y = (ycount/points->GetCount()); | |
1fc25a89 JS |
546 | } |
547 | ||
548 | /* | |
549 | * Check that (x1, y1) -> (x2, y2) hits (x3, y3) -> (x4, y4). | |
550 | * If so, ratio1 gives the proportion along the first line | |
551 | * that the intersection occurs (or something like that). | |
552 | * Used by functions below. | |
553 | * | |
554 | */ | |
555 | void oglCheckLineIntersection(double x1, double y1, double x2, double y2, | |
556 | double x3, double y3, double x4, double y4, | |
557 | double *ratio1, double *ratio2) | |
558 | { | |
559 | double denominator_term = (y4 - y3)*(x2 - x1) - (y2 - y1)*(x4 - x3); | |
560 | double numerator_term = (x3 - x1)*(y4 - y3) + (x4 - x3)*(y1 - y3); | |
561 | ||
562 | double line_constant; | |
563 | double length_ratio = 1.0; | |
564 | double k_line = 1.0; | |
565 | ||
566 | // Check for parallel lines | |
567 | if ((denominator_term < 0.005) && (denominator_term > -0.005)) | |
568 | line_constant = -1.0; | |
569 | else | |
570 | line_constant = numerator_term/denominator_term; | |
571 | ||
572 | // Check for intersection | |
573 | if ((line_constant < 1.0) && (line_constant > 0.0)) | |
574 | { | |
575 | // Now must check that other line hits | |
576 | if (((y4 - y3) < 0.005) && ((y4 - y3) > -0.005)) | |
577 | k_line = ((x1 - x3) + line_constant*(x2 - x1))/(x4 - x3); | |
578 | else | |
579 | k_line = ((y1 - y3) + line_constant*(y2 - y1))/(y4 - y3); | |
580 | ||
581 | if ((k_line >= 0.0) && (k_line < 1.0)) | |
582 | length_ratio = line_constant; | |
583 | else | |
584 | k_line = 1.0; | |
585 | } | |
586 | *ratio1 = length_ratio; | |
587 | *ratio2 = k_line; | |
588 | } | |
589 | ||
590 | /* | |
591 | * Find where (x1, y1) -> (x2, y2) hits one of the lines in xvec, yvec. | |
592 | * (*x3, *y3) is the point where it hits. | |
593 | * | |
594 | */ | |
595 | void oglFindEndForPolyline(double n, double xvec[], double yvec[], | |
596 | double x1, double y1, double x2, double y2, double *x3, double *y3) | |
597 | { | |
598 | int i; | |
599 | double lastx = xvec[0]; | |
600 | double lasty = yvec[0]; | |
601 | ||
602 | double min_ratio = 1.0; | |
603 | double line_ratio; | |
604 | double other_ratio; | |
605 | ||
606 | for (i = 1; i < n; i++) | |
607 | { | |
608 | oglCheckLineIntersection(x1, y1, x2, y2, lastx, lasty, xvec[i], yvec[i], | |
609 | &line_ratio, &other_ratio); | |
610 | lastx = xvec[i]; | |
611 | lasty = yvec[i]; | |
612 | ||
613 | if (line_ratio < min_ratio) | |
614 | min_ratio = line_ratio; | |
615 | } | |
616 | ||
617 | // Do last (implicit) line if last and first doubles are not identical | |
618 | if (!(xvec[0] == lastx && yvec[0] == lasty)) | |
619 | { | |
620 | oglCheckLineIntersection(x1, y1, x2, y2, lastx, lasty, xvec[0], yvec[0], | |
621 | &line_ratio, &other_ratio); | |
622 | ||
623 | if (line_ratio < min_ratio) | |
624 | min_ratio = line_ratio; | |
625 | } | |
626 | ||
627 | *x3 = (x1 + (x2 - x1)*min_ratio); | |
628 | *y3 = (y1 + (y2 - y1)*min_ratio); | |
629 | ||
630 | } | |
631 | ||
632 | /* | |
633 | * Find where the line hits the box. | |
634 | * | |
635 | */ | |
636 | ||
637 | void oglFindEndForBox(double width, double height, | |
638 | double x1, double y1, // Centre of box (possibly) | |
639 | double x2, double y2, // other end of line | |
640 | double *x3, double *y3) // End on box edge | |
641 | { | |
642 | double xvec[5]; | |
643 | double yvec[5]; | |
644 | ||
645 | xvec[0] = (double)(x1 - width/2.0); | |
646 | yvec[0] = (double)(y1 - height/2.0); | |
647 | xvec[1] = (double)(x1 - width/2.0); | |
648 | yvec[1] = (double)(y1 + height/2.0); | |
649 | xvec[2] = (double)(x1 + width/2.0); | |
650 | yvec[2] = (double)(y1 + height/2.0); | |
651 | xvec[3] = (double)(x1 + width/2.0); | |
652 | yvec[3] = (double)(y1 - height/2.0); | |
653 | xvec[4] = (double)(x1 - width/2.0); | |
654 | yvec[4] = (double)(y1 - height/2.0); | |
655 | ||
656 | oglFindEndForPolyline(5, xvec, yvec, x2, y2, x1, y1, x3, y3); | |
657 | } | |
658 | ||
659 | /* | |
660 | * Find where the line hits the circle. | |
661 | * | |
662 | */ | |
663 | ||
664 | void oglFindEndForCircle(double radius, | |
665 | double x1, double y1, // Centre of circle | |
666 | double x2, double y2, // Other end of line | |
667 | double *x3, double *y3) | |
668 | { | |
669 | double H = (double)sqrt((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1)); | |
670 | ||
671 | if (H == 0.0) | |
672 | { | |
673 | *x3 = x1; | |
674 | *y3 = y1; | |
675 | } | |
676 | else | |
677 | { | |
678 | *y3 = radius * (y2 - y1)/H + y1; | |
679 | *x3 = radius * (x2 - x1)/H + x1; | |
680 | } | |
681 | } | |
682 | ||
683 | /* | |
684 | * Given the line (x1, y1) -> (x2, y2), and an arrow size of given length and width, | |
685 | * return the position of the tip of the arrow and the left and right vertices of the arrow. | |
686 | * | |
687 | */ | |
688 | ||
689 | void oglGetArrowPoints(double x1, double y1, double x2, double y2, | |
690 | double length, double width, | |
691 | double *tip_x, double *tip_y, | |
692 | double *side1_x, double *side1_y, | |
693 | double *side2_x, double *side2_y) | |
694 | { | |
695 | double l = (double)sqrt((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1)); | |
696 | ||
697 | if (l < 0.01) | |
698 | l = (double) 0.01; | |
699 | ||
700 | double i_bar = (x2 - x1)/l; | |
701 | double j_bar = (y2 - y1)/l; | |
702 | ||
703 | double x3 = (- length*i_bar) + x2; | |
704 | double y3 = (- length*j_bar) + y2; | |
705 | ||
706 | *side1_x = width*(-j_bar) + x3; | |
707 | *side1_y = width*i_bar + y3; | |
708 | ||
709 | *side2_x = -width*(-j_bar) + x3; | |
710 | *side2_y = -width*i_bar + y3; | |
711 | ||
712 | *tip_x = x2; *tip_y = y2; | |
713 | } | |
714 | ||
715 | /* | |
716 | * Given an ellipse and endpoints of a line, returns the point at which | |
717 | * the line touches the ellipse in values x4, y4. | |
718 | * This function assumes that the centre of the ellipse is at x1, y1, and the | |
719 | * ellipse has a width of width1 and a height of height1. It also assumes you are | |
720 | * wanting to draw an arc FROM point x2, y2 TOWARDS point x3, y3. | |
721 | * This function calculates the x,y coordinates of the intersection point of | |
722 | * the arc with the ellipse. | |
723 | * Author: Ian Harrison | |
724 | */ | |
725 | ||
726 | void oglDrawArcToEllipse(double x1, double y1, double width1, double height1, double x2, double y2, double x3, double y3, | |
727 | double *x4, double *y4) | |
728 | { | |
729 | double a1 = (double)(width1/2.0); | |
730 | double b1 = (double)(height1/2.0); | |
731 | ||
732 | // These are required to give top left x and y coordinates for DrawEllipse | |
733 | // double top_left_x1 = (double)(x1 - a1); | |
734 | // double top_left_y1 = (double)(y1 - b1); | |
735 | /* | |
736 | // Check for vertical line | |
737 | if (fabs(x2 - x3) < 0.05) | |
738 | { | |
739 | *x4 = x3; | |
740 | if (y2 < y3) | |
741 | *y4 = (double)(y1 - b1); | |
742 | else | |
743 | *y4 = (double)(y1 + b1); | |
744 | return; | |
745 | } | |
746 | */ | |
747 | // Check that x2 != x3 | |
748 | if (fabs(x2 - x3) < 0.05) | |
749 | { | |
750 | *x4 = x2; | |
751 | if (y3 > y2) | |
752 | *y4 = (double)(y1 - sqrt((b1*b1 - (((x2-x1)*(x2-x1))*(b1*b1)/(a1*a1))))); | |
753 | else | |
754 | *y4 = (double)(y1 + sqrt((b1*b1 - (((x2-x1)*(x2-x1))*(b1*b1)/(a1*a1))))); | |
755 | return; | |
756 | } | |
757 | ||
758 | // Calculate the x and y coordinates of the point where arc intersects ellipse | |
759 | ||
760 | double A, B, C, D, E, F, G, H, K; | |
761 | double ellipse1_x, ellipse1_y; | |
762 | ||
763 | A = (double)(1/(a1 * a1)); | |
764 | B = (double)((y3 - y2) * (y3 - y2)) / ((x3 - x2) * (x3 - x2) * b1 * b1); | |
765 | C = (double)(2 * (y3 - y2) * (y2 - y1)) / ((x3 - x2) * b1 * b1); | |
766 | D = (double)((y2 - y1) * (y2 - y1)) / (b1 * b1); | |
767 | E = (double)(A + B); | |
768 | F = (double)(C - (2 * A * x1) - (2 * B * x2)); | |
769 | G = (double)((A * x1 * x1) + (B * x2 * x2) - (C * x2) + D - 1); | |
770 | H = (double)((y3 - y2) / (x3 - x2)); | |
771 | K = (double)((F * F) - (4 * E * G)); | |
772 | ||
773 | if (K >= 0) | |
774 | // In this case the line intersects the ellipse, so calculate intersection | |
775 | { | |
776 | if(x2 >= x1) | |
777 | { | |
778 | ellipse1_x = (double)(((F * -1) + sqrt(K)) / (2 * E)); | |
779 | ellipse1_y = (double)((H * (ellipse1_x - x2)) + y2); | |
780 | } | |
781 | else | |
782 | { | |
783 | ellipse1_x = (double)(((F * -1) - sqrt(K)) / (2 * E)); | |
784 | ellipse1_y = (double)((H * (ellipse1_x - x2)) + y2); | |
785 | } | |
786 | } | |
787 | else | |
788 | // in this case, arc does not intersect ellipse, so just draw arc | |
789 | { | |
790 | ellipse1_x = x3; | |
791 | ellipse1_y = y3; | |
792 | } | |
793 | *x4 = ellipse1_x; | |
794 | *y4 = ellipse1_y; | |
795 | ||
796 | /* | |
797 | // Draw a little circle (radius = 2) at the end of the arc where it hits | |
798 | // the ellipse . | |
799 | ||
800 | double circle_x = ellipse1_x - 2.0; | |
801 | double circle_y = ellipse1_y - 2.0; | |
802 | m_canvas->DrawEllipse(circle_x, circle_y, 4.0, 4.0); | |
803 | */ | |
804 | } | |
805 | ||
806 | // Update a list item from a list of strings | |
807 | void UpdateListBox(wxListBox *item, wxList *list) | |
808 | { | |
809 | item->Clear(); | |
810 | if (!list) | |
811 | return; | |
812 | ||
b9ac87bc | 813 | wxNode *node = list->GetFirst(); |
1fc25a89 JS |
814 | while (node) |
815 | { | |
b9ac87bc | 816 | char *s = (char *)node->GetData(); |
1fc25a89 | 817 | item->Append(s); |
fd657b8a | 818 | node = node->GetNext(); |
1fc25a89 JS |
819 | } |
820 | } | |
821 | ||
822 | bool oglRoughlyEqual(double val1, double val2, double tol) | |
823 | { | |
824 | return ( (val1 < (val2 + tol)) && (val1 > (val2 - tol)) && | |
825 | (val2 < (val1 + tol)) && (val2 > (val1 - tol))); | |
826 | } | |
827 | ||
828 | /* | |
829 | * Hex<->Dec conversion | |
830 | */ | |
831 | ||
832 | // Array used in DecToHex conversion routine. | |
833 | static char sg_HexArray[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', | |
834 | 'C', 'D', 'E', 'F' }; | |
835 | ||
836 | // Convert 2-digit hex number to decimal | |
837 | unsigned int oglHexToDec(char* buf) | |
838 | { | |
839 | int firstDigit, secondDigit; | |
840 | ||
841 | if (buf[0] >= 'A') | |
842 | firstDigit = buf[0] - 'A' + 10; | |
843 | else | |
844 | firstDigit = buf[0] - '0'; | |
845 | ||
846 | if (buf[1] >= 'A') | |
847 | secondDigit = buf[1] - 'A' + 10; | |
848 | else | |
849 | secondDigit = buf[1] - '0'; | |
850 | ||
851 | return firstDigit * 16 + secondDigit; | |
852 | } | |
853 | ||
854 | // Convert decimal integer to 2-character hex string | |
855 | void oglDecToHex(unsigned int dec, char *buf) | |
856 | { | |
857 | int firstDigit = (int)(dec/16.0); | |
858 | int secondDigit = (int)(dec - (firstDigit*16.0)); | |
859 | buf[0] = sg_HexArray[firstDigit]; | |
860 | buf[1] = sg_HexArray[secondDigit]; | |
861 | buf[2] = 0; | |
862 | } | |
863 | ||
864 | // 3-digit hex to wxColour | |
865 | wxColour oglHexToColour(const wxString& hex) | |
866 | { | |
867 | if (hex.Length() == 6) | |
868 | { | |
869 | char buf[7]; | |
870 | strncpy(buf, hex, 7); | |
871 | unsigned int r = oglHexToDec((char *)buf); | |
872 | unsigned int g = oglHexToDec((char *)(buf+2)); | |
873 | unsigned int b = oglHexToDec((char *)(buf+4)); | |
874 | return wxColour(r, g, b); | |
875 | } | |
876 | else | |
877 | return wxColour(0,0,0); | |
878 | } | |
879 | ||
880 | // RGB to 3-digit hex | |
881 | wxString oglColourToHex(const wxColour& colour) | |
882 | { | |
883 | char buf[7]; | |
884 | unsigned int red = colour.Red(); | |
885 | unsigned int green = colour.Green(); | |
886 | unsigned int blue = colour.Blue(); | |
887 | ||
888 | oglDecToHex(red, buf); | |
889 | oglDecToHex(green, buf+2); | |
890 | oglDecToHex(blue, buf+4); | |
891 | ||
892 | return wxString(buf); | |
893 | } | |
894 | ||
895 |