Skip to content

Commit ff4e6e9

Browse files
committed
abstracted out shapes
1 parent 453cf68 commit ff4e6e9

File tree

5 files changed

+132
-108
lines changed

5 files changed

+132
-108
lines changed

Vector/vector_shapes.c

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#include <math.h>
2+
3+
#include "vector_shapes.h"
4+
#include "vector_display.h"
5+
6+
int
7+
vector_shape_draw_line(vector_display_t *display, double x0, double y0, double x1, double y1)
8+
{
9+
vector_display_begin_draw(display, x0, y0);
10+
vector_display_draw_to(display, x1, y1);
11+
vector_display_end_draw(display);
12+
return 0;
13+
}
14+
15+
int
16+
vector_shape_draw_wheel(vector_display_t *display, double angle, double x, double y, double radius)
17+
{
18+
double spokeradius = radius - 2.0f;
19+
// draw spokes
20+
vector_shape_draw_line(display,
21+
x + spokeradius * sin(angle),
22+
y - spokeradius * cos(angle),
23+
x - spokeradius * sin(angle),
24+
y + spokeradius * cos(angle));
25+
vector_shape_draw_line(display,
26+
x + spokeradius * sin(angle + M_PI / 4.0f),
27+
y - spokeradius * cos(angle + M_PI / 4.0f),
28+
x - spokeradius * sin(angle + M_PI / 4.0f),
29+
y + spokeradius * cos(angle + M_PI / 4.0f));
30+
vector_shape_draw_line(display,
31+
x + spokeradius * sin(angle + M_PI / 2.0f),
32+
y - spokeradius * cos(angle + M_PI / 2.0f),
33+
x - spokeradius * sin(angle + M_PI / 2.0f),
34+
y + spokeradius * cos(angle + M_PI / 2.0f));
35+
vector_shape_draw_line(display,
36+
x + spokeradius * sin(angle + 3.0f * M_PI / 4.0f),
37+
y - spokeradius * cos(angle + 3.0f * M_PI / 4.0f),
38+
x - spokeradius * sin(angle + 3.0f * M_PI / 4.0f),
39+
y + spokeradius * cos(angle + 3.0f * M_PI / 4.0f));
40+
41+
double edgeangle = 0.0f;
42+
double angadjust = 0.0f;
43+
44+
vector_display_begin_draw(display,
45+
x + radius * sin(angle + edgeangle + angadjust),
46+
y - radius * cos(angle + edgeangle + angadjust));
47+
for (edgeangle = 0; edgeangle < 2 * M_PI - 0.001; edgeangle += M_PI/4.0f) {
48+
vector_display_draw_to(display,
49+
x + radius * sin(angle + edgeangle + M_PI / 4.0f - angadjust),
50+
y - radius * cos(angle + edgeangle + M_PI / 4.0f - angadjust));
51+
}
52+
vector_display_end_draw(display);
53+
return 0;
54+
}
55+
56+
int
57+
vector_shape_draw_circle(vector_display_t *display, double x, double y, double radius, double steps)
58+
{
59+
double edgeangle = 0.0f;
60+
double angadjust = 0.0f;
61+
62+
double step = M_PI * 2 / steps;
63+
64+
vector_display_begin_draw(display,
65+
x + radius * sin(edgeangle + angadjust),
66+
y - radius * cos(edgeangle + angadjust));
67+
for (edgeangle = 0; edgeangle < 2 * M_PI - 0.001; edgeangle += step) {
68+
vector_display_draw_to(display,
69+
x + radius * sin(edgeangle + step - angadjust),
70+
y - radius * cos(edgeangle + step - angadjust));
71+
}
72+
vector_display_end_draw(display);
73+
return 0;
74+
}
75+
76+
int
77+
vector_shape_draw_box(vector_display_t *display, double x, double y, double w, double h)
78+
{
79+
vector_display_begin_draw(display, x, y);
80+
vector_display_draw_to(display, x + w, y);
81+
vector_display_draw_to(display, x + w, y + h);
82+
vector_display_draw_to(display, x, y + h);
83+
vector_display_draw_to(display, x, y);
84+
vector_display_end_draw(display);
85+
return 0;
86+
}

Vector/vector_shapes.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#ifndef INCLUDED_VECTOR_SHAPES_H
2+
#define INCLUDED_VECTOR_SHAPES_H
3+
4+
#include "vector_display.h"
5+
6+
int vector_shape_draw_line (vector_display_t *display, double x0, double y0, double x1, double y1);
7+
int vector_shape_draw_box (vector_display_t *display, double x, double y, double w, double h);
8+
int vector_shape_draw_circle (vector_display_t *display, double x, double y, double radius, double steps);
9+
int vector_shape_draw_wheel (vector_display_t *display, double spokeangle, double x, double y, double radius);
10+
11+
#endif

ios/Vector.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
/* Begin PBXBuildFile section */
1010
3052245216BCD70C000D3D44 /* VectorTestImpl.c in Sources */ = {isa = PBXBuildFile; fileRef = 3052245016BCD70C000D3D44 /* VectorTestImpl.c */; };
11+
30C963AB16BE1A9B00805A37 /* vector_shapes.c in Sources */ = {isa = PBXBuildFile; fileRef = 30C963AA16BE1A9B00805A37 /* vector_shapes.c */; };
1112
D34DA911165F069300AEA9C2 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D34DA910165F069300AEA9C2 /* UIKit.framework */; };
1213
D34DA913165F069300AEA9C2 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D34DA912165F069300AEA9C2 /* Foundation.framework */; };
1314
D34DA915165F069300AEA9C2 /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D34DA914165F069300AEA9C2 /* CoreGraphics.framework */; };
@@ -29,6 +30,8 @@
2930
3052245016BCD70C000D3D44 /* VectorTestImpl.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = VectorTestImpl.c; path = ../test/VectorTestImpl.c; sourceTree = "<group>"; };
3031
3052245116BCD70C000D3D44 /* VectorTestImpl.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = VectorTestImpl.h; path = ../test/VectorTestImpl.h; sourceTree = "<group>"; };
3132
3052245416BCD760000D3D44 /* vector_display_glinc.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = vector_display_glinc.h; sourceTree = "<group>"; };
33+
30C963A916BE1A9B00805A37 /* vector_shapes.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = vector_shapes.h; sourceTree = "<group>"; };
34+
30C963AA16BE1A9B00805A37 /* vector_shapes.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = vector_shapes.c; sourceTree = "<group>"; };
3235
D34DA90C165F069300AEA9C2 /* Vector.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Vector.app; sourceTree = BUILT_PRODUCTS_DIR; };
3336
D34DA910165F069300AEA9C2 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; };
3437
D34DA912165F069300AEA9C2 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
@@ -116,6 +119,8 @@
116119
D34DA916165F069300AEA9C2 /* Vector */ = {
117120
isa = PBXGroup;
118121
children = (
122+
30C963A916BE1A9B00805A37 /* vector_shapes.h */,
123+
30C963AA16BE1A9B00805A37 /* vector_shapes.c */,
119124
3052245416BCD760000D3D44 /* vector_display_glinc.h */,
120125
3052245016BCD70C000D3D44 /* VectorTestImpl.c */,
121126
3052245116BCD70C000D3D44 /* VectorTestImpl.h */,
@@ -217,6 +222,7 @@
217222
D3779FB516925E08008C7653 /* vector_font_simplex.c in Sources */,
218223
D382C7161697C8EE00BF7D64 /* vector_display_utils.c in Sources */,
219224
3052245216BCD70C000D3D44 /* VectorTestImpl.c in Sources */,
225+
30C963AB16BE1A9B00805A37 /* vector_shapes.c in Sources */,
220226
);
221227
runOnlyForDeploymentPostprocessing = 0;
222228
};

osx/VectorOSXTest.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
3052244816BCD305000D3D44 /* vector_display.c in Sources */ = {isa = PBXBuildFile; fileRef = 3052243F16BCD305000D3D44 /* vector_display.c */; };
1818
3052244A16BCD305000D3D44 /* vector_display_utils.c in Sources */ = {isa = PBXBuildFile; fileRef = 3052244316BCD305000D3D44 /* vector_display_utils.c */; };
1919
3052244B16BCD305000D3D44 /* vector_font_simplex.c in Sources */ = {isa = PBXBuildFile; fileRef = 3052244616BCD305000D3D44 /* vector_font_simplex.c */; };
20+
30C963A316BDFC9A00805A37 /* vector_shapes.c in Sources */ = {isa = PBXBuildFile; fileRef = 30C963A216BDFC9A00805A37 /* vector_shapes.c */; };
2021
/* End PBXBuildFile section */
2122

2223
/* Begin PBXFileReference section */
@@ -43,6 +44,8 @@
4344
3052244516BCD305000D3D44 /* vector_display.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = vector_display.h; path = ../Vector/vector_display.h; sourceTree = "<group>"; };
4445
3052244616BCD305000D3D44 /* vector_font_simplex.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = vector_font_simplex.c; path = ../Vector/vector_font_simplex.c; sourceTree = "<group>"; };
4546
3052244716BCD305000D3D44 /* vector_font_simplex.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = vector_font_simplex.h; path = ../Vector/vector_font_simplex.h; sourceTree = "<group>"; };
47+
30C963A116BDFC9A00805A37 /* vector_shapes.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = vector_shapes.h; path = ../Vector/vector_shapes.h; sourceTree = "<group>"; };
48+
30C963A216BDFC9A00805A37 /* vector_shapes.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = vector_shapes.c; path = ../Vector/vector_shapes.c; sourceTree = "<group>"; };
4649
/* End PBXFileReference section */
4750

4851
/* Begin PBXFrameworksBuildPhase section */
@@ -121,6 +124,8 @@
121124
3052243B16BCD2CB000D3D44 /* Vector */ = {
122125
isa = PBXGroup;
123126
children = (
127+
30C963A116BDFC9A00805A37 /* vector_shapes.h */,
128+
30C963A216BDFC9A00805A37 /* vector_shapes.c */,
124129
3052243F16BCD305000D3D44 /* vector_display.c */,
125130
3052244016BCD305000D3D44 /* vector_display_glinc.h */,
126131
3052244316BCD305000D3D44 /* vector_display_utils.c */,
@@ -204,6 +209,7 @@
204209
3052244816BCD305000D3D44 /* vector_display.c in Sources */,
205210
3052244A16BCD305000D3D44 /* vector_display_utils.c in Sources */,
206211
3052244B16BCD305000D3D44 /* vector_font_simplex.c in Sources */,
212+
30C963A316BDFC9A00805A37 /* vector_shapes.c in Sources */,
207213
);
208214
runOnlyForDeploymentPostprocessing = 0;
209215
};

test/VectorTestImpl.c

Lines changed: 23 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -5,95 +5,12 @@
55
#include "vector_display_glinc.h"
66
#include "vector_display.h"
77
#include "vector_font_simplex.h"
8+
#include "vector_shapes.h"
89

910
static vector_display_t *display;
1011
static int dwidth;
1112
static int dheight;
1213

13-
static double scale = 1.0;
14-
static int offsetx;
15-
static int offsety;
16-
17-
static void draw_line(vector_display_t *display, double x0, double y0, double x1, double y1) {
18-
vector_display_begin_draw(display, x0, y0);
19-
vector_display_draw_to(display, x1, y1);
20-
vector_display_end_draw(display);
21-
}
22-
23-
static void draw_wheel(vector_display_t *display, double angle, double x, double y, double radius) {
24-
double spokeradius = radius - 2.0f;
25-
// draw spokes
26-
draw_line(display,
27-
x + spokeradius * sin(angle),
28-
y - spokeradius * cos(angle),
29-
x - spokeradius * sin(angle),
30-
y + spokeradius * cos(angle));
31-
draw_line(display,
32-
x + spokeradius * sin(angle + M_PI / 4.0f),
33-
y - spokeradius * cos(angle + M_PI / 4.0f),
34-
x - spokeradius * sin(angle + M_PI / 4.0f),
35-
y + spokeradius * cos(angle + M_PI / 4.0f));
36-
draw_line(display,
37-
x + spokeradius * sin(angle + M_PI / 2.0f),
38-
y - spokeradius * cos(angle + M_PI / 2.0f),
39-
x - spokeradius * sin(angle + M_PI / 2.0f),
40-
y + spokeradius * cos(angle + M_PI / 2.0f));
41-
draw_line(display,
42-
x + spokeradius * sin(angle + 3.0f * M_PI / 4.0f),
43-
y - spokeradius * cos(angle + 3.0f * M_PI / 4.0f),
44-
x - spokeradius * sin(angle + 3.0f * M_PI / 4.0f),
45-
y + spokeradius * cos(angle + 3.0f * M_PI / 4.0f));
46-
47-
double edgeangle = 0.0f;
48-
double angadjust = 0.0f;
49-
50-
vector_display_begin_draw(display,
51-
x + radius * sin(angle + edgeangle + angadjust),
52-
y - radius * cos(angle + edgeangle + angadjust));
53-
for (edgeangle = 0; edgeangle < 2 * M_PI - 0.001; edgeangle += M_PI/4.0f) {
54-
vector_display_draw_to(display,
55-
x + radius * sin(angle + edgeangle + M_PI / 4.0f - angadjust),
56-
y - radius * cos(angle + edgeangle + M_PI / 4.0f - angadjust));
57-
}
58-
vector_display_end_draw(display);
59-
}
60-
61-
static void draw_circle(vector_display_t *display, double x, double y, double radius, double steps) {
62-
double edgeangle = 0.0f;
63-
double angadjust = 0.0f;
64-
65-
double step = M_PI * 2 / steps;
66-
67-
vector_display_begin_draw(display,
68-
x + radius * sin(edgeangle + angadjust),
69-
y - radius * cos(edgeangle + angadjust));
70-
for (edgeangle = 0; edgeangle < 2 * M_PI - 0.001; edgeangle += step) {
71-
vector_display_draw_to(display,
72-
x + radius * sin(edgeangle + step - angadjust),
73-
y - radius * cos(edgeangle + step - angadjust));
74-
}
75-
vector_display_end_draw(display);
76-
}
77-
78-
static void draw_box(vector_display_t *display, double x, double y, double w, double h) {
79-
vector_display_begin_draw(display, x, y);
80-
vector_display_draw_to(display, x + w, y);
81-
vector_display_draw_to(display, x + w, y + h);
82-
vector_display_draw_to(display, x, y + h);
83-
vector_display_draw_to(display, x, y);
84-
vector_display_end_draw(display);
85-
}
86-
87-
static int fixx(int x) {
88-
return (int)((double)x * scale) + offsetx;
89-
}
90-
static int fixy(int y) {
91-
return (int)((double)y * scale) + offsety;
92-
}
93-
static double fix(double v) {
94-
return v * scale;
95-
}
96-
9714
void
9815
VectorTestImpl_Draw()
9916
{
@@ -109,10 +26,10 @@ VectorTestImpl_Draw()
10926
//
11027
//vector_font_simplex_draw(display, 100, 1300, 5.0, "Hello, World!");
11128
vector_display_set_color(display, 0.7f, 0.7f, 1.0f);
112-
vector_font_simplex_draw(display, fixx(50), fixy(180), fix(3.5), "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
113-
vector_font_simplex_draw(display, fixx(50), fixy(360), fix(3.5), "abcdefghijklmnopqrstuvwxyz");
114-
vector_font_simplex_draw(display, fixx(50), fixy(540), fix(3.5), buf);
115-
vector_font_simplex_draw(display, fixx(50), fixy(720), fix(3.5), "!@#$%^&*()-=<>/?;:'\"{}[]|\\+=-_");
29+
vector_font_simplex_draw(display, (50), (180), (3.5), "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
30+
vector_font_simplex_draw(display, (50), (360), (3.5), "abcdefghijklmnopqrstuvwxyz");
31+
vector_font_simplex_draw(display, (50), (540), (3.5), buf);
32+
vector_font_simplex_draw(display, (50), (720), (3.5), "!@#$%^&*()-=<>/?;:'\"{}[]|\\+=-_");
11633

11734

11835
//
@@ -121,36 +38,36 @@ VectorTestImpl_Draw()
12138
vector_display_set_color(display, 1.0f, 0.7f, 0.7f);
12239
for (i = 0; i < 4; i++) {
12340
for (j = 0; j < i; j++) {
124-
draw_line(display, fixx(50), fixy(750 + 100 * i), fixx(400), fixy(750 + 100 * i));
41+
vector_shape_draw_line(display, (50), (750 + 100 * i), (400), (750 + 100 * i));
12542
}
12643
}
12744

12845
for (i = 0; i < 4; i++) {
12946
for (j = 0; j <= i; j++) {
130-
draw_line(display, fixx(50 + 100 * i), fixy(1200), fixx(50 + 100 * i), fixy(1400));
47+
vector_shape_draw_line(display, (50 + 100 * i), (1200), (50 + 100 * i), (1400));
13148
}
13249
}
13350

13451
//
13552
// test pattern for shapes
13653
//
13754
vector_display_set_color(display, 0.7f, 0.7f, 1.0f);
138-
draw_circle(display, fixx(500), fixy(950), fix(20), 32);
139-
draw_circle(display, fixx(600), fixy(950), fix(50), 32);
140-
draw_circle(display, fixx(800), fixy(950), fix(100), 32);
141-
draw_circle(display, fixx(1075), fixy(950), fix(150), 64);
55+
vector_shape_draw_circle(display, (500), (950), (20), 32);
56+
vector_shape_draw_circle(display, (600), (950), (50), 32);
57+
vector_shape_draw_circle(display, (800), (950), (100), 32);
58+
vector_shape_draw_circle(display, (1075), (950), (150), 64);
14259

14360
vector_display_set_color(display, 0.7f, 1.0f, 0.7f);
144-
draw_box(display, fixx(500), fixy(1200), fix(40), fix(40));
145-
draw_box(display, fixx(565), fixy(1200), fix(100), fix(100));
146-
draw_box(display, fixx(700), fixy(1200), fix(200), fix(200));
147-
draw_box(display, fixx(950), fixy(1200), fix(300), fix(300));
61+
vector_shape_draw_box(display, (500), (1200), (40), (40));
62+
vector_shape_draw_box(display, (565), (1200), (100), (100));
63+
vector_shape_draw_box(display, (700), (1200), (200), (200));
64+
vector_shape_draw_box(display, (950), (1200), (300), (300));
14865

14966
vector_display_set_color(display, 1.0f, 0.7f, 1.0f);
150-
draw_wheel(display, M_PI, fixx(1425), fixy(950), fix(150));
151-
draw_wheel(display, 3 * M_PI / 4, fixx(1700), fixy(950), fix(100));
152-
draw_wheel(display, M_PI / 2, fixx(1900), fixy(950), fix(50));
153-
draw_wheel(display, M_PI / 4, fixx(2000), fixy(950), fix(20));
67+
vector_shape_draw_wheel(display, M_PI, (1425), (950), (150));
68+
vector_shape_draw_wheel(display, 3 * M_PI / 4, (1700), (950), (100));
69+
vector_shape_draw_wheel(display, M_PI / 2, (1900), (950), (50));
70+
vector_shape_draw_wheel(display, M_PI / 4, (2000), (950), (20));
15471

15572
//
15673
// finish
@@ -171,13 +88,11 @@ static void resize(int w, int h)
17188
dheight = h;
17289

17390
if (((double)dwidth / (double)dheight) < (2048.0/1536.0)) {
174-
scale = (double)dwidth / 2048.0;
175-
offsetx = 0;
176-
offsety = (dheight - (int)((double)1536 * scale)) / 2;
91+
double scale = (double)dwidth / 2048.0;
92+
vector_display_set_transform(display, 0, (dheight - (int)((double)1536 * scale)) / 2, scale);
17793
} else {
178-
scale = (double)dheight / 1536.0;
179-
offsetx = (dwidth - (int)((double)2048 * scale)) / 2;
180-
offsety = 0;
94+
double scale = (double)dheight / 1536.0;
95+
vector_display_set_transform(display, (dwidth - (int)((double)2048 * scale)) / 2, 0, scale);
18196
}
18297
}
18398

0 commit comments

Comments
 (0)