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