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