1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
|
/*
* Smithsonian Astrophysical Observatory, Cambridge, MA, USA
* This code has been modified under the terms listed below and is made
* available under the same terms.
*/
/*
* Copyright 1993-2004 George A Howlett.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "bltGraph.h"
#include "bltGrAxis.h"
#include "bltGrElem.h"
#include "bltGrLegd.h"
#include "bltGrMisc.h"
using namespace Blt;
#define ROUND(x) ((int)((x) + (((x)<0.0) ? -0.5 : 0.5)))
#define AXIS_PAD_TITLE 2
#define ROTATE_0 0
#define ROTATE_90 1
#define ROTATE_180 2
#define ROTATE_270 3
/*
*---------------------------------------------------------------------------
*
* layoutGraph --
*
* Calculate the layout of the graph. Based upon the data, axis limits,
* X and Y titles, and title height, determine the cavity left which is
* the plotting surface. The first step get the data and axis limits for
* calculating the space needed for the top, bottom, left, and right
* margins.
*
* 1) The LEFT margin is the area from the left border to the Y axis
* (not including ticks). It composes the border width, the width an
* optional Y axis label and its padding, and the tick numeric labels.
* The Y axis label is rotated 90 degrees so that the width is the
* font height.
*
* 2) The RIGHT margin is the area from the end of the graph
* to the right window border. It composes the border width,
* some padding, the font height (this may be dubious. It
* appears to provide a more even border), the max of the
* legend width and 1/2 max X tick number. This last part is
* so that the last tick label is not clipped.
*
* Window Width
* ___________________________________________________________
* | | | |
* | | TOP height of title | |
* | | | |
* | | x2 title | |
* | | | |
* | | height of x2-axis | |
* |__________|_______________________________|_______________| W
* | | -plotpady | | i
* |__________|_______________________________|_______________| n
* | | top right | | d
* | | | | o
* | LEFT | | RIGHT | w
* | | | |
* | y | Free area = 104% | y2 | H
* | | Plotting surface = 100% | | e
* | t | Tick length = 2 + 2% | t | i
* | i | | i | g
* | t | | t legend| h
* | l | | l width| t
* | e | | e |
* | height| |height |
* | of | | of |
* | y-axis| |y2-axis |
* | | | |
* | |origin 0,0 | |
* |__________|_left_________________bottom___|_______________|
* | |-plotpady | |
* |__________|_______________________________|_______________|
* | | (xoffset, yoffset) | |
* | | | |
* | | height of x-axis | |
* | | | |
* | | BOTTOM x title | |
* |__________|_______________________________|_______________|
*
* 3) The TOP margin is the area from the top window border to the top
* of the graph. It composes the border width, twice the height of
* the title font (if one is given) and some padding between the
* title.
*
* 4) The BOTTOM margin is area from the bottom window border to the
* X axis (not including ticks). It composes the border width, the height
* an optional X axis label and its padding, the height of the font
* of the tick labels.
*
* The plotting area is between the margins which includes the X and Y axes
* including the ticks but not the tick numeric labels. The length of the
* ticks and its padding is 5% of the entire plotting area. Hence the entire
* plotting area is scaled as 105% of the width and height of the area.
*
* The axis labels, ticks labels, title, and legend may or may not be
* displayed which must be taken into account.
*
* if reqWidth > 0 : set outer size
* if reqPlotWidth > 0 : set plot size
*---------------------------------------------------------------------------
*/
void Graph::layoutGraph()
{
GraphOptions* ops = (GraphOptions*)ops_;
int titleY;
int plotWidth, plotHeight;
int inset, inset2;
int width = width_;
int height = height_;
/*
* Step 1: Compute the amount of space needed to display the axes
* associated with each margin. They can be overridden by
* -leftmargin, -rightmargin, -bottommargin, and -topmargin
* graph options, respectively.
*/
int left = getMarginGeometry(&ops->leftMargin);
int right = getMarginGeometry(&ops->rightMargin);
int top = getMarginGeometry(&ops->topMargin);
int bottom = getMarginGeometry(&ops->bottomMargin);
int pad = ops->bottomMargin.maxTickWidth;
if (pad < ops->topMargin.maxTickWidth)
pad = ops->topMargin.maxTickWidth;
pad = pad / 2 + 3;
if (right < pad)
right = pad;
if (left < pad)
left = pad;
pad = ops->leftMargin.maxTickHeight;
if (pad < ops->rightMargin.maxTickHeight)
pad = ops->rightMargin.maxTickHeight;
pad = pad / 2;
if (top < pad)
top = pad;
if (bottom < pad)
bottom = pad;
if (ops->leftMargin.reqSize > 0)
left = ops->leftMargin.reqSize;
if (ops->rightMargin.reqSize > 0)
right = ops->rightMargin.reqSize;
if (ops->topMargin.reqSize > 0)
top = ops->topMargin.reqSize;
if (ops->bottomMargin.reqSize > 0)
bottom = ops->bottomMargin.reqSize;
/*
* Step 2: Add the graph title height to the top margin.
*/
if (ops->title)
top += titleHeight_ + 6;
inset = (inset_ + ops->plotBW);
inset2 = 2 * inset;
/*
* Step 3: Estimate the size of the plot area from the remaining
* space. This may be overridden by the -plotwidth and
* -plotheight graph options. We use this to compute the
* size of the legend.
*/
if (width == 0)
width = 400;
if (height == 0)
height = 400;
plotWidth = (ops->reqPlotWidth > 0) ? ops->reqPlotWidth :
width - (inset2 + left + right); /* Plot width. */
plotHeight = (ops->reqPlotHeight > 0) ? ops->reqPlotHeight :
height - (inset2 + top + bottom); /* Plot height. */
legend_->map(plotWidth, plotHeight);
/*
* Step 2: Add the legend to the appropiate margin.
*/
if (!legend_->isHidden()) {
switch (legend_->position()) {
case Legend::RIGHT:
right += legend_->width() + 2;
break;
case Legend::LEFT:
left += legend_->width() + 2;
break;
case Legend::TOP:
top += legend_->height() + 2;
break;
case Legend::BOTTOM:
bottom += legend_->height() + 2;
break;
case Legend::XY:
case Legend::PLOT:
/* Do nothing. */
break;
}
}
/*
* Recompute the plotarea or graph size, now accounting for the legend.
*/
if (ops->reqPlotWidth == 0) {
plotWidth = width - (inset2 + left + right);
if (plotWidth < 1)
plotWidth = 1;
}
if (ops->reqPlotHeight == 0) {
plotHeight = height - (inset2 + top + bottom);
if (plotHeight < 1)
plotHeight = 1;
}
/*
* Step 5: If necessary, correct for the requested plot area aspect
* ratio.
*/
if ((ops->reqPlotWidth == 0) && (ops->reqPlotHeight == 0) &&
(ops->aspect > 0.0f)) {
float ratio;
/*
* Shrink one dimension of the plotarea to fit the requested
* width/height aspect ratio.
*/
ratio = (float)plotWidth / (float)plotHeight;
if (ratio > ops->aspect) {
// Shrink the width
int scaledWidth = (int)(plotHeight * ops->aspect);
if (scaledWidth < 1)
scaledWidth = 1;
// Add the difference to the right margin.
// CHECK THIS: w = scaledWidth
right += (plotWidth - scaledWidth);
}
else {
// Shrink the height
int scaledHeight = (int)(plotWidth / ops->aspect);
if (scaledHeight < 1)
scaledHeight = 1;
// Add the difference to the top margin
// CHECK THIS: h = scaledHeight;
top += (plotHeight - scaledHeight);
}
}
/*
* Step 6: If there's multiple axes in a margin, the axis titles will be
* displayed in the adjoining margins. Make sure there's room
* for the longest axis titles.
*/
if (top < ops->leftMargin.axesTitleLength)
top = ops->leftMargin.axesTitleLength;
if (right < ops->bottomMargin.axesTitleLength)
right = ops->bottomMargin.axesTitleLength;
if (top < ops->rightMargin.axesTitleLength)
top = ops->rightMargin.axesTitleLength;
if (right < ops->topMargin.axesTitleLength)
right = ops->topMargin.axesTitleLength;
/*
* Step 7: Override calculated values with requested margin sizes.
*/
if (ops->leftMargin.reqSize > 0)
left = ops->leftMargin.reqSize;
if (ops->rightMargin.reqSize > 0)
right = ops->rightMargin.reqSize;
if (ops->topMargin.reqSize > 0)
top = ops->topMargin.reqSize;
if (ops->bottomMargin.reqSize > 0)
bottom = ops->bottomMargin.reqSize;
if (ops->reqPlotWidth > 0) {
/*
* Width of plotarea is constained. If there's extra space, add it to
* th left and/or right margins. If there's too little, grow the
* graph width to accomodate it.
*/
int w = plotWidth + inset2 + left + right;
if (width > w) { /* Extra space in window. */
int extra = (width - w) / 2;
if (ops->leftMargin.reqSize == 0) {
left += extra;
if (ops->rightMargin.reqSize == 0) {
right += extra;
}
else {
left += extra;
}
}
else if (ops->rightMargin.reqSize == 0) {
right += extra + extra;
}
}
else if (width < w) {
width = w;
}
}
if (ops->reqPlotHeight > 0) { /* Constrain the plotarea height. */
/*
* Height of plotarea is constained. If there's extra space,
* add it to th top and/or bottom margins. If there's too little,
* grow the graph height to accomodate it.
*/
int h = plotHeight + inset2 + top + bottom;
if (height > h) { /* Extra space in window. */
int extra;
extra = (height - h) / 2;
if (ops->topMargin.reqSize == 0) {
top += extra;
if (ops->bottomMargin.reqSize == 0) {
bottom += extra;
}
else {
top += extra;
}
}
else if (ops->bottomMargin.reqSize == 0) {
bottom += extra + extra;
}
}
else if (height < h) {
height = h;
}
}
width_ = width;
height_ = height;
left_ = left + inset;
top_ = top + inset;
right_ = width - right - inset;
bottom_ = height - bottom - inset;
ops->leftMargin.width = left + inset_;
ops->rightMargin.width = right + inset_;
ops->topMargin.height = top + inset_;
ops->bottomMargin.height = bottom + inset_;
vOffset_ = top_ + ops->yPad;
vRange_ = plotHeight - 2*ops->yPad;
hOffset_ = left_ + ops->xPad;
hRange_ = plotWidth - 2*ops->xPad;
if (vRange_ < 1)
vRange_ = 1;
if (hRange_ < 1)
hRange_ = 1;
hScale_ = 1.0f / (float)hRange_;
vScale_ = 1.0f / (float)vRange_;
// Calculate the placement of the graph title so it is centered within the
// space provided for it in the top margin
titleY = titleHeight_;
titleY_ = 3 + inset_;
titleX_ = (right_ + left_) / 2;
}
int Graph::getMarginGeometry(Margin *marginPtr)
{
GraphOptions* ops = (GraphOptions*)ops_;
int isHoriz = !(marginPtr->site & 0x1); /* Even sites are horizontal */
// Count the visible axes.
unsigned int nVisible = 0;
unsigned int l =0;
int w =0;
int h =0;
marginPtr->maxTickWidth =0;
marginPtr->maxTickHeight =0;
if (ops->stackAxes) {
for (Blt_ChainLink link = Blt_Chain_FirstLink(marginPtr->axes);
link != NULL; link = Blt_Chain_NextLink(link)) {
Axis* axisPtr = (Axis*)Blt_Chain_GetValue(link);
AxisOptions* ops = (AxisOptions*)axisPtr->ops();
if (!ops->hide && axisPtr->use_) {
nVisible++;
if (flags & GET_AXIS_GEOMETRY)
getAxisGeometry(axisPtr);
if (isHoriz) {
if (h < axisPtr->height_)
h = axisPtr->height_;
}
else {
if (w < axisPtr->width_)
w = axisPtr->width_;
}
if (axisPtr->maxTickWidth_ > marginPtr->maxTickWidth)
marginPtr->maxTickWidth = axisPtr->maxTickWidth_;
if (axisPtr->maxTickHeight_ > marginPtr->maxTickHeight)
marginPtr->maxTickHeight = axisPtr->maxTickHeight_;
}
}
}
else {
for (Blt_ChainLink link = Blt_Chain_FirstLink(marginPtr->axes);
link != NULL; link = Blt_Chain_NextLink(link)) {
Axis* axisPtr = (Axis*)Blt_Chain_GetValue(link);
AxisOptions* ops = (AxisOptions*)axisPtr->ops();
if (!ops->hide && axisPtr->use_) {
nVisible++;
if (flags & GET_AXIS_GEOMETRY)
getAxisGeometry(axisPtr);
if ((ops->titleAlternate) && (l < axisPtr->titleWidth_))
l = axisPtr->titleWidth_;
if (isHoriz)
h += axisPtr->height_;
else
w += axisPtr->width_;
if (axisPtr->maxTickWidth_ > marginPtr->maxTickWidth)
marginPtr->maxTickWidth = axisPtr->maxTickWidth_;
if (axisPtr->maxTickHeight_ > marginPtr->maxTickHeight)
marginPtr->maxTickHeight = axisPtr->maxTickHeight_;
}
}
}
// Enforce a minimum size for margins.
if (w < 3)
w = 3;
if (h < 3)
h = 3;
marginPtr->nAxes = nVisible;
marginPtr->axesTitleLength = l;
marginPtr->width = w;
marginPtr->height = h;
marginPtr->axesOffset = (isHoriz) ? h : w;
return marginPtr->axesOffset;
}
void Graph::getAxisGeometry(Axis *axisPtr)
{
GraphOptions* ops = (GraphOptions*)ops_;
AxisOptions* aops = (AxisOptions*)axisPtr->ops();
axisPtr->freeTickLabels();
// Leave room for axis baseline and padding
unsigned int y =0;
if (aops->exterior && (ops->plotRelief != TK_RELIEF_SOLID))
y += aops->lineWidth + 2;
axisPtr->maxTickHeight_ = axisPtr->maxTickWidth_ = 0;
if (axisPtr->t1Ptr_)
free(axisPtr->t1Ptr_);
axisPtr->t1Ptr_ = axisPtr->generateTicks(&axisPtr->majorSweep_);
if (axisPtr->t2Ptr_)
free(axisPtr->t2Ptr_);
axisPtr->t2Ptr_ = axisPtr->generateTicks(&axisPtr->minorSweep_);
if (aops->showTicks) {
Ticks* t1Ptr = aops->t1UPtr ? aops->t1UPtr : axisPtr->t1Ptr_;
int nTicks =0;
if (t1Ptr)
nTicks = t1Ptr->nTicks;
unsigned int nLabels =0;
for (int ii=0; ii<nTicks; ii++) {
double x = t1Ptr->values[ii];
double x2 = t1Ptr->values[ii];
if (aops->labelOffset)
x2 += axisPtr->majorSweep_.step * 0.5;
if (!axisPtr->inRange(x2, &axisPtr->axisRange_))
continue;
TickLabel* labelPtr = axisPtr->makeLabel(x);
Blt_Chain_Append(axisPtr->tickLabels_, labelPtr);
nLabels++;
// Get the dimensions of each tick label. Remember tick labels
// can be multi-lined and/or rotated.
int lw, lh;
getTextExtents(aops->tickFont, labelPtr->string, -1, &lw, &lh);
labelPtr->width = lw;
labelPtr->height = lh;
if (aops->tickAngle != 0.0f) {
// Rotated label width and height
double rlw, rlh;
getBoundingBox(lw, lh, aops->tickAngle, &rlw, &rlh, NULL);
lw = ROUND(rlw), lh = ROUND(rlh);
}
if (axisPtr->maxTickWidth_ < int(lw))
axisPtr->maxTickWidth_ = lw;
if (axisPtr->maxTickHeight_ < int(lh))
axisPtr->maxTickHeight_ = lh;
}
unsigned int pad =0;
if (aops->exterior) {
/* Because the axis cap style is "CapProjecting", we need to
* account for an extra 1.5 linewidth at the end of each line. */
pad = ((aops->lineWidth * 12) / 8);
}
if (axisPtr->isHorizontal())
y += axisPtr->maxTickHeight_ + pad;
else {
y += axisPtr->maxTickWidth_ + pad;
if (axisPtr->maxTickWidth_ > 0)
// Pad either size of label.
y += 5;
}
y += 2 * AXIS_PAD_TITLE;
if ((aops->lineWidth > 0) && aops->exterior)
// Distance from axis line to tick label.
y += aops->tickLength;
} // showTicks
if (aops->title) {
if (aops->titleAlternate) {
if (y < axisPtr->titleHeight_)
y = axisPtr->titleHeight_;
}
else
y += axisPtr->titleHeight_ + AXIS_PAD_TITLE;
}
// Correct for orientation of the axis
if (axisPtr->isHorizontal())
axisPtr->height_ = y;
else
axisPtr->width_ = y;
}
void Graph::getTextExtents(Tk_Font font, const char *text, int textLen,
int* ww, int* hh)
{
if (!text) {
*ww =0;
*hh =0;
return;
}
Tk_FontMetrics fm;
Tk_GetFontMetrics(font, &fm);
int lineHeight = fm.linespace;
if (textLen < 0)
textLen = strlen(text);
int maxWidth =0;
int maxHeight =0;
int lineLen =0;
const char *line =NULL;
const char *p, *pend;
for (p = line = text, pend = text + textLen; p < pend; p++) {
if (*p == '\n') {
if (lineLen > 0) {
int lineWidth = Tk_TextWidth(font, line, lineLen);
if (lineWidth > maxWidth)
maxWidth = lineWidth;
}
maxHeight += lineHeight;
line = p + 1; /* Point to the start of the next line. */
lineLen = 0; /* Reset counter to indicate the start of a
* new line. */
continue;
}
lineLen++;
}
if ((lineLen > 0) && (*(p - 1) != '\n')) {
maxHeight += lineHeight;
int lineWidth = Tk_TextWidth(font, line, lineLen);
if (lineWidth > maxWidth)
maxWidth = lineWidth;
}
*ww = maxWidth;
*hh = maxHeight;
}
/*
*---------------------------------------------------------------------------
*
* Computes the dimensions of the bounding box surrounding a rectangle
* rotated about its center. If pointArr isn't NULL, the coordinates of
* the rotated rectangle are also returned.
*
* The dimensions are determined by rotating the rectangle, and doubling
* the maximum x-coordinate and y-coordinate.
*
* w = 2 * maxX, h = 2 * maxY
*
* Since the rectangle is centered at 0,0, the coordinates of the
* bounding box are (-w/2,-h/2 w/2,-h/2, w/2,h/2 -w/2,h/2).
*
* 0 ------- 1
* | |
* | x |
* | |
* 3 ------- 2
*
* Results:
* The width and height of the bounding box containing the rotated
* rectangle are returned.
*
*---------------------------------------------------------------------------
*/
void Graph::getBoundingBox(int width, int height, float angle,
double *rotWidthPtr, double *rotHeightPtr,
Point2d *bbox)
{
int i;
double sinTheta, cosTheta;
double radians;
double xMax, yMax;
double x, y;
Point2d corner[4];
angle = fmod(angle, 360.0);
if (fmod(angle, (double)90.0) == 0.0) {
int ll, ur, ul, lr;
double rotWidth, rotHeight;
/* Handle right-angle rotations specially. */
int quadrant = (int)(angle / 90.0);
switch (quadrant) {
case ROTATE_270: /* 270 degrees */
ul = 3, ur = 0, lr = 1, ll = 2;
rotWidth = (double)height;
rotHeight = (double)width;
break;
case ROTATE_90: /* 90 degrees */
ul = 1, ur = 2, lr = 3, ll = 0;
rotWidth = (double)height;
rotHeight = (double)width;
break;
case ROTATE_180: /* 180 degrees */
ul = 2, ur = 3, lr = 0, ll = 1;
rotWidth = (double)width;
rotHeight = (double)height;
break;
default:
case ROTATE_0: /* 0 degrees */
ul = 0, ur = 1, lr = 2, ll = 3;
rotWidth = (double)width;
rotHeight = (double)height;
break;
}
if (bbox) {
x = rotWidth * 0.5;
y = rotHeight * 0.5;
bbox[ll].x = bbox[ul].x = -x;
bbox[ur].y = bbox[ul].y = -y;
bbox[lr].x = bbox[ur].x = x;
bbox[ll].y = bbox[lr].y = y;
}
*rotWidthPtr = rotWidth;
*rotHeightPtr = rotHeight;
return;
}
/* Set the four corners of the rectangle whose center is the origin. */
corner[1].x = corner[2].x = (double)width * 0.5;
corner[0].x = corner[3].x = -corner[1].x;
corner[2].y = corner[3].y = (double)height * 0.5;
corner[0].y = corner[1].y = -corner[2].y;
radians = (-angle / 180.0) * M_PI;
sinTheta = sin(radians), cosTheta = cos(radians);
xMax = yMax = 0.0;
/* Rotate the four corners and find the maximum X and Y coordinates */
for (i = 0; i < 4; i++) {
x = (corner[i].x * cosTheta) - (corner[i].y * sinTheta);
y = (corner[i].x * sinTheta) + (corner[i].y * cosTheta);
if (x > xMax)
xMax = x;
if (y > yMax)
yMax = y;
if (bbox) {
bbox[i].x = x;
bbox[i].y = y;
}
}
/*
* By symmetry, the width and height of the bounding box are twice the
* maximum x and y coordinates.
*/
*rotWidthPtr = xMax + xMax;
*rotHeightPtr = yMax + yMax;
}
/*
*---------------------------------------------------------------------------
*
* Blt_AnchorPoint --
*
* Translates a position, using both the dimensions of the bounding box,
* and the anchor direction, returning the coordinates of the upper-left
* corner of the box. The anchor indicates where the given x-y position
* is in relation to the bounding box.
*
* 7 nw --- 0 n --- 1 ne
* | |
* 6 w 8 center 2 e
* | |
* 5 sw --- 4 s --- 3 se
*
* The coordinates returned are translated to the origin of the bounding
* box (suitable for giving to XCopyArea, XCopyPlane, etc.)
*
* Results:
* The translated coordinates of the bounding box are returned.
*
*---------------------------------------------------------------------------
*/
Point2d Graph::anchorPoint(double x, double y, double w, double h,
Tk_Anchor anchor)
{
Point2d t;
switch (anchor) {
case TK_ANCHOR_NW: /* 7 Upper left corner */
break;
case TK_ANCHOR_W: /* 6 Left center */
y -= (h * 0.5);
break;
case TK_ANCHOR_SW: /* 5 Lower left corner */
y -= h;
break;
case TK_ANCHOR_N: /* 0 Top center */
x -= (w * 0.5);
break;
case TK_ANCHOR_CENTER: /* 8 Center */
x -= (w * 0.5);
y -= (h * 0.5);
break;
case TK_ANCHOR_S: /* 4 Bottom center */
x -= (w * 0.5);
y -= h;
break;
case TK_ANCHOR_NE: /* 1 Upper right corner */
x -= w;
break;
case TK_ANCHOR_E: /* 2 Right center */
x -= w;
y -= (h * 0.5);
break;
case TK_ANCHOR_SE: /* 3 Lower right corner */
x -= w;
y -= h;
break;
}
t.x = x;
t.y = y;
return t;
}
|