-
Notifications
You must be signed in to change notification settings - Fork 4
/
PAINTCON.CPP
2221 lines (1975 loc) · 74.8 KB
/
PAINTCON.CPP
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
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//******************************************************************
// P A I N T C O N
//
// Functions primarily concerned with displaying graphics (not text).
//
// Copyright (c) 1993 - 2005 by John Coulthard
//
// This file is part of QuikGrid.
//
// QuikGrid is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// QuikGrid is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with QuikGrid (File gpl.txt); if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
// or visit their website at http://www.fsf.org/licensing/licenses/gpl.txt .
//
// Sept. 23/98: Insert code to support grid resolution.
// Jan. 18/99: Improve xyInfo2d by making sure point being sought
// is visible - prevents program expection.
// Jan. 19/99: Add InTheGrid function (is mouse in the grid?).
// Jan. 29/99: Change display of coordinate info to use common functions.
// Feb. 4/99: Implement 3d axes.
// Only draw visible grid in 2d mode.
// Feb. 10/99: Add ZoomIn and ZoomOut.
// Feb. 13/99: Add panning support for arrow keys.
// Feb. 14/99: Always display mark if comment is present.
// Display axes if selected.
// Mar. 2/99: Change BluePen and ScatPen to use 0 width (1 pizel).
// (means skinny lines at all times).
// Change Mark to scale to window size.
// Mar. 23/99: Change to label/highlight starting at 0'th line
// (instead of at the highlight'th line).
// Change to have NiceContourLinesSet handle secondary line concept.
// Mar. 28/99: Change ContourLinesSet to set contour lines starting at zmin.
// Change NiceContoursSet to reinitialize #contours and # between.
// May 22/99: Remove bug - right mouse click bad for any grid resolution.
// May 25-27/99: Improve drawing speed (check line ends for visible & buffer).
// Jun 4/99: Fix bug in PaintSurfaceRatio - error checking upper limit.
// Jun 5/99: Changed CloseTo2d code to do direct calculation (speed increase).
// Jun 5/99: Move hdc communication with QuikGrid to global extern. Fix
// but in optimization code having to do with pen updates.
// Jun 11/99: Moving code over to drawstuf.cpp. (Polybuffer, Plotto).
// Oct 25/99: Initial code to show meterology stuff on display.
// Oct 29/99: Change to make contour lines optional in 2d mode.
// Feb. 2/00: Add support for outlines.
// Mar, 26/00: Check for outline or data points not on the defined grid.
// Jun 13/00: Assert( xyRange == 0 ) checks commented out.
// if condition sets xyRange to 1 in these circumstances.
// It appears that xyRange will drop to zero under circumstances
// where an outline is loaded for a grid and the two differ so
// extremely in magnitude that underflows occur. This is probably
// a user error - but I am not sure how it can easily be detected in a
// straight forward fashion. Also never had a field report on it so
// it probably is not very important to address this "problem?"
// Jun 15/00: Made Margin and shift available globally so they could be changed
// on the fly from a command file.
// Sep 5/00: check for xyRange checks for <= xyRangeMin (10.0) instead of zero.
// Sep 11/00: Try xyRangeMin of 2 to correct scaling when outline only loaded.
// Sep 25/00: Moving contouring stuff to condraw.cpp.
// Nov. 4/00: Moving references to ContourLines[i] to ContourLineValue function
// Nov 8-12/00: Many changes to allow custom contour colours.
// Nov 26/00: Make the outline a bold pen.
// Dec. 4/00: Make outline bold an option.
// Feb. 14/01: 3D outline was always bold - fixed!
// Mar. 13/01: Implement Axes always on top.
// Implement ColourLegend
// Mar. 14/01: Implement DataPointsOnGridOnly
// Oct. 3/01: Fix bug in colour legend if no grid to display.
// Dec. 4/01: Change precision for the display of the z coordinate
// for corner intersections or comment attached to data point
// from 3 to 5 (why I set it to 3 originally I can't remember
// but suspect it was due to small screens of the day).
// Sep. 01/09: Implement user control of speed arrow length.
// (create "ArrowSize" external).
//*********************************************************************
//
// Candidates to move to external source module.
// DoLineTo,
// Moving save2d and save3d lines to global would help.
#include <windows.h>
#include <iostream>
#include <strstream>
#include <iomanip>
#include <math.h>
#include <string.h>
#include <stdio.h>
#include "assert.h"
#include "surfgrid.h"
#include "xygrid.h"
#include "scatdata.h"
#include "saveline.h"
#include "xpand.h"
#include "quikgrid.h"
#include "paintcon.h"
#include "loaddata.h"
#include "dxfwrite.h"
#include "utilitys.h"
#include "rc.h"
#include "rotate.h"
#include "contour.h"
#include "hatch.h"
#include "labelc.h"
#include "drawstuf.h"
#include "condraw.h"
using namespace std;
extern HWND WindProcHwnd;
extern HWND hGridLocationDlgBox;
extern HDC hdc;
extern int Zooming;
extern ContourOptionsType DrawingOptions;
extern int LatLonData;
extern int GridGeneratedOK;
extern int GridLock;
extern ScatData OutLine;
extern SaveLineType Save2dContours,
Save3dContours;
extern int ColourContours;
extern int ColouredGrid;
extern float CgridZadjust;
extern int BoldOutline;
extern UINT PaintState;
extern bool CreatingAMetaFile;
extern int QuikGridChangedImage;
extern int NeverHideContours;
extern int LabelNDigits; // Defined in Labelc.cpp
extern double ColourZmin, ColourZmid, ColourZmax;
// Global definition grids:
SurfaceGrid Sgrid(2,2), // The meteorological speed grid.
Dgrid(2,2), // The meteorological direction grid (0-360).
Cgrid(2,2); // The colour grid.
float ZoomRatio = 1.5 ; // Default amount of change on zoom.
float PanRatio = 0.1; // Default for panning with arrow keys.
int IncludeColourLegend = TRUE;
int DataPointsOnGridOnly = TRUE; // Only show scattered data points if they
// are on the defined grid area.
int SmallMark = TRUE; // Size of the mark for the data point.
int DisplayVolumeDifference = 1;
int TextSize = 80;
float ArrowSize = 2.; // Arrow length twice maximum grid side.
float ZmaxLabel = 0.0, ZminLabel = 0.0; // For drawing the axes
//*************************************************************************
extern double VolumeDifference,
VolumeDifferencePositive,
VolumeDifferenceNegative;
extern int LabelNthLine,
AxesOnTop = FALSE;
//*************************************************************************
static xyGridClass xyGrid(2,2); // initial dimensions only.
static int xyGridOK = 0;
static const int MarkSize = 30, // length of a tic to mark a point.
MarkFraction = MarkSize/10;
static ContourType ContourMode = TwoD;
static int BlackAndWhite;
//static HBRUSH hBrush;
static int SpecialPenInUse = FALSE;
static int SpecialPenWasUsed = FALSE;
static long ColourLegendBoxSize;
static long ColourLegendBoxSizeInPixels = 20;
static int BoldWidth; // Width of a bold line - dependant on window size.
HPEN hGridPen,
hArrowPen,
hContourPen,
hBoldContourPen,
hDataPointPen,
hOutlinePen,
hConnectPointsPen,
hBlackPen,
hOldPen,
hBoldPen,
hBackgroundPen,
hAxisPen,
hSpecialPen,
hPenInUse;
COLORREF GridPen = RGB( 0, 0, 255),
ArrowPen = RGB( 0, 0, 255),
ContourPen = RGB( 0, 0, 0),
BoldContourPen = RGB( 0, 0, 0),
DataPointPen = RGB( 255, 0, 0),
OutlinePen = RGB( 255, 0, 0),
ConnectPointsPen = RGB( 255, 0, 0),
BlackPen = RGB( 0, 0, 0),
BackgroundPen = RGB( 255, 255, 255 ),
TextPen = RGB( 0, 0, 0),
AxisPen = RGB( 0, 0, 0);
static HFONT hFont;
static int ContourValueIndex;
static float ContourValue = 0.,
Real3DContourValue,
zRatio = .6;
// xmin... yMax define the limits of the current image in "user" units.
// (not screen units). For the 2d view this is normally the same as the
// extents for the Z grid. For the 3d view this is the extents of the
// grid after rotating, including adjustment for the Zratio.
static float xMin, xMax, yMin, yMax;
static float zMin, zMax,
xRange,
yRange,
zRange,
xyRange;
// XminZgrid... zMaxZgrid define the extents of the unrotated native
// Z grid.
static float xMinZgrid, xMaxZgrid,
yMinZgrid, yMaxZgrid,
zMinZgrid, zMaxZgrid;
float Margin = 400.0,
shift = Margin*2;
static const float MaxScale = 6000.0,
// What is the following? I think it was created to prevent
// underflows when the xyRange was close to zero. I'm not even
// sure it is needed - it can result in too small a display.
xyRangeMin = 0.000010; //* Previously 0.01
static int PercentzRatio = 60,
TitleXposn = Margin,
TitleYposn = 0, // will be replaced.
PenMode = 1;
static long GridInfoIX,
GridInfoIY;
static inline int ScaleX(float x)
{ return ( x - xMin)/xyRange*MaxScale + Margin;}
static inline int ScaleY(float y) // Flip y to go from 6000 -> 0;
{ return -( ( y - yMin)/xyRange*MaxScale + Margin) + MaxScale+shift;}
static inline float unScaleX( int ix )
{ return (ix-Margin)*xyRange/MaxScale + xMin; }
static inline float unScaleY( int iy )
{ return (-(iy-MaxScale-shift)-Margin)*xyRange/MaxScale + yMin; }
struct PORT {
int xr; // x range
int yr; // y range
int xo; // x origin
int yo; // y origin
} ;
static PORT CurrWin,
CurrView,
ZoomWin,
PreviousWin,
PreviousView;
RECT ClientRect, ClipRect;
//********************************************************
// C r e a t e P e n s
//********************************************************
void CreatePens( )
{
hGridPen = CreatePen( PS_SOLID, 0, GridPen);
hArrowPen = CreatePen( PS_SOLID, 0, ArrowPen);
hContourPen = CreatePen( PS_SOLID, 0, ContourPen);
hBoldContourPen = CreatePen( PS_SOLID, 0, BoldContourPen);
hDataPointPen = CreatePen( PS_SOLID, 0, DataPointPen);
hOutlinePen = CreatePen( PS_SOLID, 0, OutlinePen);
hConnectPointsPen = CreatePen( PS_SOLID, 0, ConnectPointsPen);
hBlackPen = CreatePen( PS_SOLID, 0, ContourPen);
hAxisPen = CreatePen( PS_SOLID, 0, AxisPen);
hBackgroundPen = (HPEN)CreateSolidBrush( BackgroundPen ) ;
}
//********************************************************
// D e s t r o y P e n s
//********************************************************
void DestroyPens()
{
DeleteObject( hGridPen );
DeleteObject( hArrowPen );
DeleteObject( hContourPen );
DeleteObject( hBoldContourPen );
DeleteObject( hDataPointPen );
DeleteObject( hOutlinePen );
DeleteObject( hConnectPointsPen );
DeleteObject( hBlackPen);
DeleteObject( hAxisPen);
DeleteObject( hBackgroundPen);
}
//*********************************************************
// S e t N e w W i n
//*********************************************************
static void SetNewWin( HDC &hdc, PORT NewWin )
{
SetWindowExtEx( hdc, NewWin.xr, NewWin.yr, NULL );
SetWindowOrgEx( hdc, NewWin.xo, NewWin.yo, NULL );
PreviousWin = CurrWin;
CurrWin = NewWin;
}
//*********************************************************
// S e t N e w V i e w
//*********************************************************
static void SetNewView( HDC &hdc, PORT NewView )
{
SetViewportExtEx( hdc, NewView.xr, NewView.yr, NULL );
SetViewportOrgEx( hdc, NewView.xo, NewView.yo, NULL );
PreviousView = CurrView;
CurrView = NewView;
}
//********************************************************
// S e t N e w W i n V i e w
//********************************************************
static void SetNewWinView( HDC &hdc, PORT NewWin, PORT NewView )
{
SetNewWin( hdc, NewWin);
SetNewView( hdc, NewView);
}
//*********************************************************
// S e t C u r r e n t W i n V i e w
//*********************************************************
void SetCurrentWinView( HDC &hdc )
{
SetWindowExtEx( hdc, CurrWin.xr, CurrWin.yr, NULL );
SetWindowOrgEx( hdc, CurrWin.xo, CurrWin.yo, NULL );
SetViewportExtEx( hdc, CurrView.xr, CurrView.yr, NULL );
SetViewportOrgEx( hdc, CurrView.xo, CurrView.yo, NULL );
}
//********************************************************
// S e t C u r r e n t W i n
//********************************************************
void SetCurrentWin( HDC &hdc )
{
SetWindowExtEx( hdc, CurrWin.xr, CurrWin.yr, NULL );
SetWindowOrgEx( hdc, CurrWin.xo, CurrWin.yo, NULL );
}
//********************************************************
// Z o o m V i e w P r e v i o u s
//********************************************************
void ZoomViewPrevious()
{
ZoomWin = PreviousWin;
}
//********************************************************
// Z o o m V i e w D o u b l e
//********************************************************
int ZoomViewDouble( const POINT ZoomCentre, const int ZoomFactor )
{
static int TempZoom;
static float TempZoomRatio;
static POINT OldZoomCentre;
OldZoomCentre.x = ZoomWin.xo + ZoomWin.xr/2;
OldZoomCentre.y = ZoomWin.yo+ZoomWin.yr/2;
TempZoom = ZoomFactor;
assert( ZoomFactor != 0 );
if( ZoomWin.xr < 200 && ZoomFactor > 0 ) TempZoom = 1;
if( ZoomWin.xr > 8000 && ZoomFactor < 0 ) TempZoom = -1;
if( abs( TempZoom) == 1 && OldZoomCentre.x == ZoomCentre.x &&
OldZoomCentre.y == ZoomCentre.y ) return 0;
TempZoomRatio = ZoomRatio;
if( abs( TempZoom ) == 1 ) TempZoomRatio = 1. ;
if( TempZoom < 0 ) TempZoomRatio = -TempZoomRatio;
if( ZoomFactor > 0 )
{
assert( TempZoomRatio > 0.0 );
ZoomWin.xr = ZoomWin.xr/TempZoomRatio;
ZoomWin.yr = ZoomWin.yr/TempZoomRatio;
}
else // ZoomFactor < 0
{
ZoomWin.xr = ZoomWin.xr*(-TempZoomRatio);
ZoomWin.yr = ZoomWin.yr*(-TempZoomRatio);
}
ZoomWin.xo = ZoomCentre.x - ZoomWin.xr/2;
ZoomWin.yo = ZoomCentre.y - ZoomWin.yr/2;
return 1;
}
//**************************************************************
// Z o o m I n
//**************************************************************
int ZoomIn( )
{
static POINT ZoomCentre;
ZoomCentre.x = ZoomWin.xo + ZoomWin.xr/2;
ZoomCentre.y = ZoomWin.yo + ZoomWin.yr/2;
return ZoomViewDouble( ZoomCentre, 2 );
}
//**************************************************************
// Z o o m O u t
//**************************************************************
int ZoomOut( )
{
static POINT ZoomCentre;
ZoomCentre.x = ZoomWin.xo+ZoomWin.xr/2;
ZoomCentre.y = ZoomWin.yo+ZoomWin.yr/2;
return ZoomViewDouble( ZoomCentre, -2 );
}
//**************************************************************
// P A N V I E W
//**************************************************************
int PanView( const int xDirection, const int yDirection )
{
static POINT ZoomCentre;
static int xMotion, yMotion;
ZoomCentre.x = ZoomWin.xo + ZoomWin.xr/2;
ZoomCentre.y = ZoomWin.yo + ZoomWin.yr/2;
xMotion = ZoomWin.xr*PanRatio*2;
yMotion = ZoomWin.yr*PanRatio*2;
ZoomCentre.x = ZoomCentre.x + xMotion*xDirection;
ZoomCentre.y = ZoomCentre.y + yMotion*yDirection;
return ZoomViewDouble( ZoomCentre, 1 ) ;
}
//********************************************************
// G e t Z o o m e d U s e r C o o r d
//********************************************************
int GetZoomedUser( float &xmin, float &xmax,
float &ymin, float &ymax )
{
xmin = unScaleX( ClientRect.left );
xmax = unScaleX( ClientRect.right );
ymin = unScaleY( ClientRect.bottom );
ymax = unScaleY( ClientRect.top );
// Constrain to the space of the actual data.
xmin = max( xmin, ScatterData.xMin());
xmax = min( xmax, ScatterData.xMax());
ymin = max( ymin, ScatterData.yMin());
ymax = min( ymax, ScatterData.yMax());
return 1;
}
//********************************************************
// V i s i b l e
//
// Returns TRUE if x,y are inside the clipping window
//********************************************************
int Visible( const int &ix, const int &iy )
{
if( CreatingAMetaFile ) return 1;
if( ix < ClipRect.left ) return 0;
if( iy < ClipRect.top ) return 0;
if( ix > ClipRect.right ) return 0;
if( iy > ClipRect.bottom ) return 0;
return 1;
}
//********************************************************
// V i s i b l e X
//
// Returns -1 0 +1 if X is less than equal or greater than.
//********************************************************
int VisibleX( const int &ix )
{
if( CreatingAMetaFile ) return 0;
if( ix < ClipRect.left ) return -1;
if( ix > ClipRect.right ) return 1;
return 0;
}
//********************************************************
// V i s i b l e Y
//
// Returns -1 0 +1 if Y is less than equal or greater than.
//********************************************************
int VisibleY( const int &iy )
{
if( CreatingAMetaFile ) return 0;
if( iy < ClipRect.top ) return -1;
if( iy > ClipRect.bottom ) return 1;
return 0;
}
//********************************************************
// I n T h e G r i d
//
// Returns true if x,y are inside the defined grid.
//********************************************************
int InTheGrid( const int &ix, const int &iy )
// Screen position integer version.
{
static float x, y;
if( !Visible( ix, iy ) ) return 0;
y = unScaleY( iy );
x = unScaleX( ix );
if( x < Zgrid.xmin() ) return 0;
if( y < Zgrid.ymin() ) return 0;
if( x > Zgrid.xmax() ) return 0;
if( y > Zgrid.ymax() ) return 0;
return 1;
}
static int InTheGrid( const float x, const float y )
// Internal grid only.
// Check floating point x and y
{
if( x < xMinZgrid ) return 0;
if( y < yMinZgrid ) return 0;
if( x > xMaxZgrid ) return 0;
if( y > yMaxZgrid ) return 0;
return 1;
}
//********************************************************
// P a i n t S u r f a c e R a t i o: Get and Set zRatio
//********************************************************
int PaintSurfaceRatio() { return PercentzRatio; }
int PaintSurfaceRatio( int NewRatio )
{ float OldRatio = PercentzRatio;
PercentzRatio = NewRatio ;
if( PercentzRatio < 0 ) PercentzRatio = 0;
if( PercentzRatio > 200 ) PercentzRatio = 200;
zRatio = (float) PercentzRatio*.01;
RotateReset();
Save3dContours.Reset();
return OldRatio;
}
//********************************************************
// A p p l y z R a t i o
//********************************************************
static void ApplyzRatio()
{
// A zRatio of zero is used within QuikGrid as meaning to
// use unscaled units, which is not how the zratio function
// of the class works. It expects a zero ratio to mean unscaled
// z coordinates. So this function maps 0. to -1.0.
if( zRatio == 0.0 ) Zgrid.zratio( -1.0 ) ; // unscaled z units.
else Zgrid.zratio( zRatio );
}
//********************************************************
// S e t E x t e n t s
//********************************************************
void SetExtents( HDC &hdc )
{
if( xyRange <= xyRangeMin ) xyRange = xyRangeMin;
static PORT Extents;
Extents.xr = ScaleX(xMax)-ScaleX(xMin) + shift;
Extents.yr = abs(ScaleY(yMax)-ScaleY(yMin)) + shift;
Extents.xo = 0;
Extents.yo = ScaleY(yMax) - Margin;
SetNewWin( hdc, Extents );
}
//********************************************************
// C e n t r e D i s p l a y
//********************************************************
void CentreDisplay( HDC &hdc, RECT &rect, RECT &cliprect, int Zooming )
// rect is the view rectangle for the entire client area of the window.
// cliprect is the update or clipping rectangle
{
static PORT CentreView;
static int CentreShift;
if( xyRange <= xyRangeMin ) xyRange = xyRangeMin; // Try to avoid scaling under/over flows
SetMapMode( hdc, MM_ISOTROPIC );
// Save coordinates of rectangle
ClientRect = rect;
ClipRect = cliprect;
//char szBuffer[200];
//sprintf( szBuffer, "Client rectangle: %i %i %i %i Clip rectangle %i %i %i %i",
// ClientRect.left, ClientRect.right, ClientRect.bottom, ClientRect.top,
// ClipRect.left, ClipRect.right, ClipRect.bottom, ClipRect.top);
//NotifyUser( szBuffer );
if( !Zooming ) SetExtents( hdc ); // Set new window if not zooming.
CentreView.xr = rect.right;
CentreView.yr = rect.bottom;
// Centre the thing in the window.
if( rect.bottom <= 0 ) // Pathological case - null height.
{ CentreView.xo = CentreView.yo = 0;
SetNewView( hdc, CentreView ); } // Non centering call.
else
{
if( (float)rect.right/rect.bottom < (float)CurrWin.xr/CurrWin.yr )
{ CentreView.xo = 0; // tall
CentreShift = (rect.bottom-(rect.right*(long)CurrWin.yr)/CurrWin.xr)/2;
CentreView.yo = CentreShift;
}
else // wide
{ CentreShift = (rect.right-(rect.bottom*(long)CurrWin.xr)/CurrWin.yr)/2;
CentreView.xo = CentreShift;
CentreView.yo = 0;
}
if( !Zooming ) SetNewView( hdc, CentreView );
else SetNewWinView( hdc, ZoomWin, CentreView );
}
DPtoLP( hdc, (LPPOINT)&ClientRect, 2);
DPtoLP( hdc, (LPPOINT)&ClipRect, 2);
//hBrush = CreateSolidBrush( BackgroundPen ) ;
FillRect( hdc, &ClientRect, (HBRUSH)hBackgroundPen );
ZoomWin = CurrWin;
//assert( DeleteObject( hBrush));
}
//********************************************************
// R o t a t e G r i d R e s e t
//********************************************************
void RotateReset() { xyGridOK = 0; }
//********************************************************
// R o t a t e Grid
//********************************************************
// Routine to rotate zGrid and save as int 2d grid.
void RotateGrid()
{
if( xyGridOK ) return;
xyGridOK = 1;
float x, y, z;
int xsize = Zgrid.xsize();
int ysize = Zgrid.ysize();
xyGrid.New( xsize, ysize);
for( int i = 0; i<xsize; i++ )
{
for( int j = 0; j<ysize; j++)
{
z = Zgrid.z(i,j);
if ( z < 0.0 ) xyGrid.set( i, j, 0, 0 );
else
{
x = Zgrid.x(i);
y = Zgrid.y(j);
Rotate( y, x, z);
if( Projection != 0.0 ) Project( x, y, z);
xyGrid.set( i, j, ScaleX( x ), ScaleY( y ) );
}
}
}
}
//********************************************************
// S c a l e 2 d R e c t a n g l e
//********************************************************
void Scale2dRectangle()
{
xMin = Zgrid.xmin();
yMin = Zgrid.ymin();
xMax = Zgrid.xmax();
yMax = Zgrid.ymax();
xRange = xMax - xMin;
yRange = yMax - yMin;
xyRange = max( xRange , yRange);
if( xyRange <= xyRangeMin ) xyRange = xyRangeMin;
assert( xyRange > 0.0 );
// NotifyUser( "Xmin-max %f %f ", xMin, xMax);
// NotifyUser( "Ymin-max %f %f", xMin, xMax) ;
}
//**********************************************************
// D r a w 2 d G r i d L i n e
//**********************************************************
// Service routine.
// Delete multiple moves and draws.
static void Draw2dGridLine( int xScaled, int yScaled, float z, int &Draw )
{
static int xOld, yOld,
DrawOld = 0;
if( DrawOld != Draw ) // new scan line started.
{
LineTo( hdc, xOld, yOld );
DrawOld = 0;
}
if( z<0.0 ) // z forces Draw to 0
{
if( DrawOld ) LineTo( hdc, xOld, yOld );
Draw = 0;
}
else // z > 0.0
{
if( !DrawOld ) MoveToEx( hdc, xScaled, yScaled, NULL );
Draw = 1;
}
xOld = xScaled; yOld = yScaled; DrawOld = Draw;
}
//********************************************************
// F o r m a t D a t a P o i n t T e x t
//********************************************************
char* FormatDataPointText( long i )
{
static double xadjust, yadjust;
char szTemp[30];
ostrstream Buf;
Buf << setprecision(5) ;
if( DrawingOptions.pointnumber ) Buf << (i+1) ;
if( DrawingOptions.xyplot )
{
LoadNormalization(xadjust, yadjust);
FormatXY( szTemp, sizeof(szTemp), ScatterData.x(i)+xadjust,
ScatterData.y(i)+yadjust ) ;
if( DrawingOptions.pointnumber ) Buf << "," ;
Buf << szTemp;
}
if( DrawingOptions.numbers)
{
if( DrawingOptions.pointnumber) Buf << ",";
Buf << " " << (ScatterData.z(i)-ScatterData.zAdjust() );
}
if( DrawingOptions.comments )
{
if( ScatterData.comment(i)[0] != NULL )
{
if( DrawingOptions.pointnumber || DrawingOptions.numbers )
Buf << "," ;
// Buf << "\"" << ScatterData.comment(i) << "\"" ;
if( DrawingOptions.pointnumber || DrawingOptions.numbers ||
DrawingOptions.xyplot ) Buf << "\"" ;
Buf << ScatterData.comment(i) ;
if( DrawingOptions.pointnumber || DrawingOptions.numbers ||
DrawingOptions.xyplot ) Buf << "\"" ;
}
}
Buf << ends;
return Buf.str();
}
//********************************************************
// D r a w M a r k
//********************************************************
// ... and xy position and z value and comments if selected.
// and the point number if selected.
//
static void DrawMark(const int &x, const int &y, const long &i )
{
static char *szBuf;
static int ScaledMarkSize, SmallWidth;
ScaledMarkSize = CurrWin.xr/90;
SmallWidth = BoldWidth/128;
if( SmallWidth < 1 ) SmallWidth = 1;
if( ScaledMarkSize < 2 ) ScaledMarkSize = 2;
if( ScaledMarkSize > MarkSize ) ScaledMarkSize = MarkSize;
if( !Visible( x, y ) ) return;
if( DrawingOptions.showmarks ||
(DrawingOptions.comments && ScatterData.comment(i)[0] != NULL ) )
{
if( SmallMark )
{
MoveToEx( hdc, x, y, NULL); LineTo( hdc, x+SmallWidth, y);
MoveToEx( hdc, x, y, NULL); LineTo( hdc, x, y+SmallWidth);
}
else
{
MoveToEx( hdc, x-ScaledMarkSize, y, NULL); LineTo( hdc, x+ScaledMarkSize, y);
MoveToEx( hdc, x, y-ScaledMarkSize, NULL); LineTo( hdc, x, y+ScaledMarkSize);
}
}
if( DrawingOptions.pointnumber||
DrawingOptions.numbers||
DrawingOptions.comments||
DrawingOptions.xyplot)
{
szBuf = FormatDataPointText(i);
TextOut( hdc, x+MarkFraction, y+MarkFraction, szBuf, strlen(szBuf) );
delete szBuf;
} // end if DrawingOptions ...
}
//********************************************************
// C l o s e T o 2 d G r i d
//
// Finds the closest grid intersection to (x,y) and returns
// it's coordinates as (IX,IY). The distance between (x,y)
// and (IX,IY), squared, is returned as the function value.
//********************************************************
static float CloseTo2dGrid( const float x, const float y, long &IX, long &IY )
{
static float xDistance, yDistance, incr;
static const float round = 0.5;
static int xSize, ySize;
xSize = Zgrid.xsize();
ySize = Zgrid.ysize();
incr = Zgrid.x(1)-Zgrid.x(0);
IX = (x - Zgrid.x(0))/incr + round;
if( IX < 0 ) IX = 0;
if( IX >= xSize ) IX = xSize-1;
xDistance = x - Zgrid.x(IX);
incr = Zgrid.y(1)-Zgrid.y(0);
IY = (y - Zgrid.y(0))/incr + round;
if( IY < 0 ) IY = 0;
if( IY >= ySize ) IY = ySize-1;
yDistance = y - Zgrid.y(IY);
return xDistance*xDistance + yDistance*yDistance;
}
//********************************************************
// D r a w 2 d I n t e r s e c t i o n
//********************************************************
static void Draw2dIntersection( HDC &hdc, long ix, long iy, UINT align )
{
static double xadjust, yadjust;
LoadNormalization( xadjust, yadjust );
char szTemp[50];
ostrstream Buf;
LoadNormalization(xadjust, yadjust);
FormatXY( szTemp, sizeof(szTemp), Zgrid.x(ix)+xadjust,
Zgrid.y(iy)+yadjust ) ;
Buf << setprecision(5) << szTemp ;
if( Zgrid.z(ix,iy) >= 0.0 )
Buf << " " << (Zgrid.z(ix,iy)-ScatterData.zAdjust());
Buf << ends;
char *szBuf = Buf.str();
SetTextAlign( hdc, align );
int x = ScaleX(Zgrid.x(ix));
int y = ScaleY(Zgrid.y(iy));
TextOut( hdc, x, y, szBuf, strlen(szBuf) );
delete szBuf;
MoveToEx( hdc, x-MarkSize, y, NULL); LineTo( hdc, x+MarkSize, y);
MoveToEx( hdc, x, y-MarkSize, NULL); LineTo( hdc, x, y+MarkSize);
}
//********************************************************
// D r a w 2 d C o r n e r s
//********************************************************
static void Draw2dCorners( HDC &hdc )
{
float xmin, xmax, ymin, ymax;
long ix, iy;
GetZoomedUser( xmin, xmax, ymin, ymax );
CloseTo2dGrid( xmin, ymin, ix, iy );
Draw2dIntersection( hdc, ix, iy, TA_BOTTOM );
CloseTo2dGrid( xmin, ymax, ix, iy );
Draw2dIntersection( hdc, ix, iy, TA_TOP );
CloseTo2dGrid( xmax, ymin, ix, iy );
Draw2dIntersection( hdc, ix, iy, TA_BOTTOM|TA_RIGHT );
CloseTo2dGrid( xmax, ymax, ix, iy );
Draw2dIntersection( hdc, ix, iy, TA_TOP|TA_RIGHT );
SetTextAlign( hdc, TA_TOP|TA_LEFT );
}
//****************************************************************************
// D r a w 2 d X Y A x e s
//****************************************************************************
static void Draw2dXYAxes(HDC &hdc)
{
const static int Shift = 30;
static int xmax, ymax, xmin, ymin, TextAlign;
static double xadjust, yadjust;
static char label[30];
LoadNormalization( xadjust, yadjust);
xmax = ScaleX(xMaxZgrid); // For axes on the generated grid.
ymax = ScaleY(yMaxZgrid);
xmin = ScaleX(xMinZgrid);
ymin = ScaleY(yMinZgrid);
// Do the x axis. First the line.
ymax += Shift; // Shift the x axis away from the image a tiny bit.
ymin += Shift;
MoveToEx( hdc, xmin, ymin, NULL );
LineTo( hdc, xmax, ymin);
// The labels.
FormatCoord( label, sizeof(label), xMinZgrid+xadjust);
TextOut( hdc, xmin, ymin+Shift, label, strlen(label) );
TextAlign = SetTextAlign( hdc, TA_TOP|TA_LEFT );
if( LatLonData)TextOut( hdc, xmax, ymin, " E", 2 );
else TextOut( hdc, xmax, ymin, " X", 2 );
FormatCoord( label, sizeof(label), Zgrid.xmax()+xadjust);
SetTextAlign( hdc, TA_TOP|TA_RIGHT);
TextOut( hdc, xmax, ymin+Shift , label, strlen(label) );
// Now do the y axes.
xmax -= Shift; // Shift y axis back
xmin -= Shift;
ymax -= Shift; // And move the x axis.
ymin -= Shift;
MoveToEx( hdc, xmin, ymin, NULL );
LineTo( hdc, xmin, ymax );
SetTextAlign( hdc, TA_BOTTOM|TA_RIGHT);
FormatCoord( label, sizeof(label), Zgrid.ymin()+yadjust );
TextOut( hdc, xmin, ymin, label, strlen(label));
SetTextAlign( hdc, TA_BOTTOM|TA_LEFT );
if( LatLonData) TextOut( hdc, xmin, ymax, " N", 2 );
else TextOut( hdc, xmin, ymax, " Y", 2 );
SetTextAlign( hdc, TA_BOTTOM|TA_RIGHT);
FormatCoord( label, sizeof(label), Zgrid.ymax()+yadjust);
TextOut( hdc, xmin, ymax, label, strlen(label) );
TextAlign = SetTextAlign( hdc, TextAlign); //restore text alignment.
}
//****************************************************************************
// D r a w L e g e n d Box
//****************************************************************************
static void DrawLegendBox(HDC &hdc, long x, long y, double z)
{
static POINT corners[4];
static COLORREF Colour;
static HBRUSH hBrush, hOldBrush;
static HPEN hPen, hOldPen;
static char label[20];
static double zadjust;
corners[0].x = x;
corners[0].y = y;
corners[1].x = x+ColourLegendBoxSize;
corners[1].y = y;
corners[2].x = x+ColourLegendBoxSize;
corners[2].y = y+ColourLegendBoxSize;
corners[3].x = x;
corners[3].y = y+ColourLegendBoxSize;
Colour = ChooseZColour( z );
hBrush = CreateSolidBrush( Colour );
hOldBrush = (HBRUSH)SelectObject(hdc, hBrush);
hPen = CreatePen( PS_SOLID, 0, Colour);
hOldPen = (HPEN)SelectObject( hdc, hPen );
Polygon( hdc, corners, 4 );
SelectObject( hdc, hOldBrush );
assert( DeleteObject( hBrush));
SelectObject( hdc, hOldPen );
assert( DeleteObject( hPen ) );
zadjust = CgridZadjust;
sprintf( label, "%.5g", (double) z-zadjust );
TextOut( hdc, x+ColourLegendBoxSize*3/2,
y, label, strlen(label) ) ;
}
//************************************************************
// Z g r i d I s D i s p l a y a b l e
//***********************************************************
static bool ZgridIsDisplayable()
{
static int xSize, ySize, i, j;
if( Zgrid.xsize() <= 3 ) return false; // there is no grid.
if( Zgrid.zmin() != Zgrid.zmax() ) return true; // a good sign coordinates are defined.
xSize = Zgrid.xsize(); // See if any grid points defined.
ySize = Zgrid.ysize();
for( i = 0; i < xSize; i++ )
{ for( j = 0; j < ySize; j++ ) if( Zgrid.z(i,j) >= 0.0 ) return true; } // if anything defined ok.