Skip to content

Commit 1081e2c

Browse files
authored
Merge pull request #181 from peterdell/dev
Have all float types at once in Snowflake sample program
2 parents ca44322 + 996799f commit 1081e2c

File tree

7 files changed

+124
-79
lines changed

7 files changed

+124
-79
lines changed

samples/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
*.lst
55
*.obx
66
*.xex
7+
*.exe
Lines changed: 49 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,69 @@
11
// Koch Snowflake
2-
//
3-
// See https://en.wikipedia.org/wiki/Koch_snowflake
2+
// https://en.wikipedia.org/wiki/Koch_snowflake
43

5-
// Float16 186 ticks
6-
// Real 197 ticks
7-
// Single 237 ticks
4+
// For every supported data type, a separate unit with the same code is used.
5+
// Float16 186 ticks
6+
// Real 197 ticks
7+
// Single 237 ticks
88

9-
program snowflake;
9+
//FPC does not have Float16
10+
{$IFNDEF FPC}
11+
{$DEFINE HAS_FLOAT16}
12+
{$ENDIF}
13+
14+
uses crt, graph, sysutils,
15+
{$IFDEF HAS_FLOAT16}
16+
snowflake_unit_float16 in 'snowflake_unit_float16.pas',
17+
{$ENDIF}
18+
snowflake_unit_real in 'snowflake_unit_real.pas',
19+
snowflake_unit_single in 'snowflake_unit_single.pas';
1020

11-
uses
12-
Crt,
13-
graph,
14-
SysUtils;
21+
procedure Snowflake;
1522

1623
const FLOAT_TYPES : array of String = [
17-
{$IFDEF Float16}
24+
{$IFDEF HAS_FLOAT16}
1825
'Float16',
1926
{$ENDIF}
2027
'Real',
2128
'Single' ];
2229

23-
var
24-
gd, gm: Smallint;
25-
26-
ticks: Cardinal;
27-
28-
{$IFDEF Float16}
29-
procedure CreateKochSnowflake_Float16;
30-
type
31-
TFloat = Float16;
32-
{$I snowflake.inc}
33-
{$ENDIF}
34-
35-
procedure CreateKochSnowflake_Real;
36-
type
37-
TFloat = Real;
38-
{$I snowflake.inc}
39-
40-
procedure CreateKochSnowflake_Single;
41-
type
42-
TFloat = Single;
43-
{$I snowflake.inc}
44-
30+
var gd, gm: SmallInt;
31+
var ticks: Cardinal;
32+
var i: Byte;
4533
var floatType: String;
4634
begin
4735

48-
for floatType in FLOAT_TYPES do
36+
for i:=Low(FLOAT_TYPES) to High(FLOAT_TYPES) do
4937
begin
5038

51-
gd := D8bit;
52-
gm := m640x480;
53-
54-
InitGraph(gd, gm, '');
55-
56-
ticks := GetTickCount;
57-
58-
{$IFDEF Float16}
59-
if floatType = 'Float16' then CreateKochSnowflake_Float16;
60-
{$ENDIF}
61-
if floatType = 'Real' then CreateKochSnowflake_Real;
62-
if floatType = 'Single' then CreateKochSnowflake_Single;
39+
gd := D8bit;
40+
gm := m640x480;
41+
42+
InitGraph(gd, gm, '');
43+
44+
ticks := GetTickCount;
45+
46+
floatType:=FLOAT_TYPES[i];
47+
{$IFDEF HAS_FLOAT16}
48+
if floatType = 'Float16' then snowflake_unit_float16.CreateKochSnowflake;
49+
{$ENDIF}
50+
if floatType = 'Real' then snowflake_unit_real.CreateKochSnowflake;
51+
if floatType = 'Single' then snowflake_unit_single.CreateKochSnowflake;
52+
53+
ticks := GetTickCount - ticks;
54+
55+
ReadKey;
56+
57+
WriteLn('Koch Snowflake with type ''',floatType,'''.');
58+
Writeln('Time required: ', ticks,' ticks');
59+
WriteLn('Press any key to continue.');
60+
ReadKey;
6361

64-
ticks := GetTickCount - ticks;
65-
66-
repeat
67-
until keypressed;
68-
69-
WriteLn('Koch Snowflake with type ''',floatType,''' required ', ticks,' ticks.');
7062
end;
63+
64+
end;
7165

72-
while True do ;
66+
begin
67+
Snowflake;
68+
end.
7369

74-
end.

samples/a8/graph_crossplatform/snowflake.inc renamed to samples/a8/graph_crossplatform/snowflake_unit.inc

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,48 @@
1-
(* Generic Koch Snowflake Include *)
2-
(* Defined TFloat as required before using this. *)
1+
2+
(* Generic Koch Snowflake Include *)
3+
(* Define TFloat as required before using this. *)
4+
5+
const
6+
center_x = 160;
7+
center_y = 115;
8+
9+
iterations = 3;
310

411
type
5-
FPoint = record
12+
TFloatPoint = record
613
x: TFloat;
714
y: TFloat;
815
end;
916

1017
const
11-
cx = 160;
12-
cy = 115;
13-
1418
ray0 = TFloat(70.0);
1519
ray1 = TFloat(ray0 / 2);
1620

1721
sqrt3 = TFloat(1.7320580756); // SQRT(3.0)
1822

19-
iterations = 3;
2023

21-
procedure LineTo2D(ax, ay: TFloat);
22-
begin
24+
procedure LineTo2D(ax, ay: TFloat);
25+
begin
2326

24-
LineTo(trunc(ax) + cx, trunc(ay) + cy);
27+
LineTo(trunc(ax) + center_x, trunc(ay) + center_y);
2528

26-
end;
29+
end;
2730

28-
procedure MoveTo2D(ax, ay: TFloat);
29-
begin
31+
procedure MoveTo2D(ax, ay: TFloat);
32+
begin
3033

31-
MoveTo(trunc(ax) + cx, trunc(ay) + cy);
34+
MoveTo(trunc(ax) + center_x, trunc(ay) + center_y);
3235

33-
end;
36+
end;
3437

35-
procedure NextSegments(ax, ay, bx, by: TFloat; n: Byte);
38+
procedure NextSegments(ax, ay, bx, by: TFloat; n: Byte);
3639
const
3740
factor: TFloat = 0.288675135; { SQRT(3) / 6 }
3841
var
39-
middle: FPoint;
42+
middle: TFloatPoint;
4043
xDelta: TFloat;
4144
yDelta: TFloat;
42-
r, s, t: FPoint;
45+
r, s, t: TFloatPoint;
4346
begin
4447

4548
if n > 0 then
@@ -78,7 +81,7 @@ const
7881

7982
end;
8083

81-
procedure KochSnowflake(a, b, c: FPoint; n: Byte);
84+
procedure KochSnowflake(a, b, c: TFloatPoint; n: Byte);
8285
begin
8386

8487
SetColor(1);
@@ -94,11 +97,11 @@ const
9497
LineTo2D(a.x, a.y);
9598
NextSegments(c.x, c.y, a.x, a.y, n);
9699

97-
end {KochSnowflake};
98-
100+
end;
99101

102+
procedure CreateKochSnowflake;
100103
var
101-
a, b, c: FPoint;
104+
a, b, c: TFloatPoint;
102105

103106
begin
104107

@@ -112,7 +115,5 @@ begin
112115
c.y := ray1 * SQRT3;
113116

114117
KochSnowflake(a, b, c, iterations);
115-
116118
end;
117119

118-
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
unit snowflake_unit_float16;
2+
3+
interface
4+
5+
procedure CreateKochSnowflake;
6+
7+
implementation
8+
9+
uses graph;
10+
11+
type
12+
TFloat = Float16;
13+
14+
{$I snowflake_unit.inc}
15+
16+
end.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
unit snowflake_unit_real;
2+
3+
interface
4+
5+
procedure CreateKochSnowflake;
6+
7+
implementation
8+
9+
uses graph;
10+
11+
type
12+
TFloat = Real;
13+
14+
{$I snowflake_unit.inc}
15+
16+
end.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
unit snowflake_unit_single;
2+
3+
interface
4+
5+
procedure CreateKochSnowflake;
6+
7+
implementation
8+
9+
uses graph;
10+
11+
type
12+
TFloat = Single;
13+
14+
{$I snowflake_unit.inc}
15+
16+
end.

src/TestUnits.pas

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
{$I Defines.inc}
44

55
uses
6-
Crt, Common, Console, Diagnostic, FileIO, MathEvaluate, Parser, Scanner, Optimize, Types, Utilities,
6+
Crt, Common, CommonTypes, Console, Diagnostic, FileIO, MathEvaluate, Parser, Scanner, Optimize, Types, Utilities,
77
SysUtils;
88

99
procedure StartTest(Name: String);

0 commit comments

Comments
 (0)