Debian Codegraph @

examples/curves/deprecated_space_filling_curve/draw_space_filling_curve.dpr

raw
1 {
2 Copyright 2003-2023 Michalis Kamburelis.
3
4 This file is part of "Castle Game Engine".
5
6 "Castle Game Engine" is free software; see the file COPYING.txt,
7 included in this distribution, for details about the copyright.
8
9 "Castle Game Engine" is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 ----------------------------------------------------------------------------
14 }
15
16 { This is a simple demo of the recursive space filling curves implemented in the
17 CastleInternalSpaceFillingCurves unit.
18
19 Always run with at least 2 command-line params:
20 curve-type (may be "peano" or "hilbert" for now)
21 curve-level (
22 for peano, reasonable example values are between 1 and 6;
23 for hilbert, reasonable example values are between 1 and 9;
24 be careful with larger levels --- the curve size grows
25 exponentially fast, as the levels are actually just the recursion depth)
26 For example
27 ./draw_space_filling_curve peano 4
28 ./draw_space_filling_curve hilbert 4
29
30 Set StepsToRedisplay variable (lower in this file) always to 1
31 to get more progressive display (you will see while drawing
32 in what order the curve is drawn, although the whole drawing
33 will be slower).
34 }
35 program draw_space_filling_curve;
36
37 {$ifdef MSWINDOWS} {$apptype GUI} {$endif}
38
39 uses SysUtils, CastleWindow, CastleUtils, CastleGLUtils, CastleParameters,
40 CastleImages, CastleVectors, Math, CastleInternalSpaceFillingCurves, CastleStringUtils, CastleGLImages,
41 CastleKeysMouse, CastleColors;
42
43 var
44 Window: TCastleWindow;
45 CurveImage: TRGBImage;
46 const
47 CurveCol: TVector3Byte = (X: 255; Y: 255; Z: 255);
48 CurveImageBGCol: TVector4Byte = (X: 0; Y: 0; Z: 0; W: 0);
49
50 { CastleWindow callbacks ------------------------------------------------------- }
51
52 procedure Render(Container: TCastleContainer);
53 var
54 DrawableImage: TDrawableImage;
55 begin
56 DrawableImage := TDrawableImage.Create(CurveImage, false, false);
57 try
58 DrawableImage.Draw(0, 0);
59 finally FreeAndNil(DrawableImage) end;
60 end;
61
62 procedure CloseQueryNotAllowed(Container: TCastleContainer); begin end;
63
64 { curve generation ------------------------------------------------------------ }
65
66 var
67 LastX: Integer = 1;
68 LastY: Integer = 1;
69 StepNum: Int64 = 0;
70 { initialized in main program }
71 StepXSize, StepYSize, StepsToRedisplay: Integer;
72
73 procedure Step(Angle: TSFCAngle; StepFuncData: Pointer);
74 var
75 x, y: Integer;
76 begin
77 x := LastX;
78 y := LastY;
79
80 case Angle of
81 0: x := x + StepXSize;
82 1: y := y + StepYSize;
83 2: x := x - StepXSize;
84 else {3:} y := y - StepYSize;
85 end;
86
87 if Between(x, 0, Integer(CurveImage.Width)-1) and
88 Between(y, 0, Integer(CurveImage.Height)-1) and
89 Between(LastX, 0, Integer(CurveImage.Width-1)) and
90 Between(LastY, 0, Integer(CurveImage.Height-1)) then
91 begin
92 if x = LastX then
93 CurveImage.VerticalLine(x, Min(y, LastY), Max(y, LastY), CurveCol)
94 else
95 CurveImage.HorizontalLine(Min(x, LastX), Max(x, LastX), y, CurveCol);
96 end;
97
98 LastX := x;
99 LastY := y;
100
101 Inc(StepNum);
102 if StepNum mod StepsToRedisplay = 0 then
103 begin
104 Window.Invalidate;
105 Application.ProcessAllMessages;
106 { Since we use TCastleWindowDemo, user is able to close the window by
107 pressing Escape. In this case we want to break the curve generation
108 and end the program. }
109 if Window.Closed then
110 Halt;
111 end;
112 end;
113
114 var
115 { vars that may be taken from params }
116 DoPeano: boolean = true;
117 Level: Cardinal = 4;
118 InitialAngle: TSFCAngle = 0;
119
120 InitialOrient: boolean; { default value depends on DoPeano }
121 StepsResolution, AllStepsCount: Cardinal;
122 begin
123 Window := TCastleWindow.Create(Application);
124
125 { parse params }
126 if Parameters.High >= 1 then
127 case ArrayPosStr(Parameters[1], ['peano', 'hilbert']) of
128 0: DoPeano := true;
129 1: DoPeano := false;
130 else raise EInvalidParams.Create('First param must be "peano" or "hilbert"');
131 end;
132 if Parameters.High >= 2 then
133 Level := StrToInt(Parameters[2]);
134 if Parameters.High >= 3 then
135 InitialAngle := StrToInt(Parameters[3]) ;
136 { if Parameters.High >= 4 then
137 InitialOrient := StrToInt(Parameters[4]) else}
138 if DoPeano then InitialOrient := false else InitialOrient := true;
139
140 { setup Window, Window.Open }
141 Window.OnRender := @Render;
142 Window.DoubleBuffer := true;
143 Window.OnCloseQuery := @CloseQueryNotAllowed;
144 Window.ParseParameters;
145 Window.SetDemoOptions(keyF11, CharEscape, true);
146 Window.Open;
147
148 { init CurveImage }
149 CurveImage := TRGBImage.Create(Window.Width, Window.Height);
150 try
151 CurveImage.Clear(CurveImageBGCol);
152
153 { calculate StepsToRedisplay i StepSize pomagajac sobie StepsResolution i
154 AllStepsCount }
155 if DoPeano then
156 StepsResolution := NatNatPower(3, Level) else
157 StepsResolution := Cardinal(1) shl Level;
158 AllStepsCount := Sqr(StepsResolution);
159 StepXSize := Max(1, CurveImage.Width div StepsResolution);
160 StepYSize := Max(1, CurveImage.Height div StepsResolution);
161 StepsToRedisplay := Max(1, Round(AllStepsCount / 20));
162 //StepsToRedisplay := 1;
163
164 { draw the curve }
165 if DoPeano then
166 PeanoCurve(InitialOrient, InitialAngle, Level, @Step, nil) else
167 HilbertCurve(InitialOrient, InitialAngle, Level, @Step, nil);
168
169 { show the window }
170 Window.Invalidate;
171 Window.OnCloseQuery := nil;
172 Application.Run;
173 finally FreeAndNil(CurveImage) end;
174 end.