Skip to content

Commit 80022b7

Browse files
committed
added extra primitives
1 parent 14d7b10 commit 80022b7

File tree

3 files changed

+53
-199
lines changed

3 files changed

+53
-199
lines changed

examples/c/primitives.c

Lines changed: 5 additions & 199 deletions
Original file line numberDiff line numberDiff line change
@@ -29,140 +29,7 @@ void blend_tile(const pp_tile_t *t) {
2929
}
3030
}
3131

32-
33-
// void _pp_round_rect_corner_points(pp_path_t *path, PP_COORD_TYPE cx, PP_COORD_TYPE cy, PP_COORD_TYPE r, int q) {
34-
// float quality = 5; // higher the number, lower the quality - selected by experiment
35-
// int steps = ceil(r / quality) + 2; // + 2 to include start and end
36-
// float delta = -(M_PI / 2) / steps;
37-
// float theta = (M_PI / 2) * q; // select start theta for this quadrant
38-
// for(int i = 0; i <= steps; i++) {
39-
// PP_COORD_TYPE xo = sin(theta) * r, yo = cos(theta) * r;
40-
// pp_path_add_point(path, (pp_point_t){cx + xo, cy + yo});
41-
// theta += delta;
42-
// }
43-
// }
44-
45-
// pp_poly_t* f_rrect(PP_COORD_TYPE x, PP_COORD_TYPE y, PP_COORD_TYPE w, PP_COORD_TYPE h, PP_COORD_TYPE tlr, PP_COORD_TYPE trr, PP_COORD_TYPE brr, PP_COORD_TYPE blr) {
46-
// pp_poly_t *poly = pp_poly_new();
47-
// pp_path_t *path = pp_poly_add_path(poly);
48-
49-
// if(tlr == 0) {
50-
// pp_path_add_point(path, (pp_point_t){x, y});
51-
// }else{
52-
// _pp_round_rect_corner_points(path, x + tlr, y + tlr, tlr, 3);
53-
// }
54-
55-
// if(trr == 0) {
56-
// pp_path_add_point(path, (pp_point_t){x + w, y});
57-
// }else{
58-
// _pp_round_rect_corner_points(path, x + w - trr, y + trr, trr, 2);
59-
// }
60-
61-
// if(brr == 0) {
62-
// pp_path_add_point(path, (pp_point_t){x + w, y + h});
63-
// }else{
64-
// _pp_round_rect_corner_points(path, x + w - brr, y + h - brr, brr, 1);
65-
// }
66-
67-
// if(blr == 0) {
68-
// pp_path_add_point(path, (pp_point_t){x, y + h});
69-
// }else{
70-
// _pp_round_rect_corner_points(path, x + blr, y + h - blr, blr, 0);
71-
// }
72-
73-
// return poly;
74-
// }
75-
76-
// pp_poly_t* rrect(PP_COORD_TYPE x, PP_COORD_TYPE y, PP_COORD_TYPE w, PP_COORD_TYPE h, PP_COORD_TYPE tlr, PP_COORD_TYPE trr, PP_COORD_TYPE brr, PP_COORD_TYPE blr, PP_COORD_TYPE t) {
77-
// pp_poly_t *outer = f_rrect(x, y, w, h, tlr, trr, brr, blr);
78-
79-
// tlr = _pp_max(0, tlr - t);
80-
// trr = _pp_max(0, trr - t);
81-
// brr = _pp_max(0, brr - t);
82-
// blr = _pp_max(0, blr - t);
83-
84-
// pp_poly_t *inner = f_rrect(x + t, y + t, w - 2 * t, h - 2 * t, tlr, trr, brr, blr);
85-
// outer->paths->next = inner->paths;
86-
// inner->paths = NULL;
87-
// free(inner);
88-
// return outer;
89-
// }
90-
91-
// pp_poly_t* f_rect(PP_COORD_TYPE x, PP_COORD_TYPE y, PP_COORD_TYPE w, PP_COORD_TYPE h) {
92-
// pp_poly_t *poly = pp_poly_new();
93-
// pp_path_t *path = pp_poly_add_path(poly);
94-
// pp_path_add_points(path, (pp_point_t[]){{x, y}, {x + w, y}, {x + w, y + h}, {x, y + h}}, 4);
95-
// return poly;
96-
// }
97-
98-
// pp_poly_t* rect(PP_COORD_TYPE x, PP_COORD_TYPE y, PP_COORD_TYPE w, PP_COORD_TYPE h, PP_COORD_TYPE t) {
99-
// pp_poly_t *poly = pp_poly_new();
100-
// pp_path_t *outer = pp_poly_add_path(poly), *inner = pp_poly_add_path(poly);
101-
// pp_path_add_points(outer, (pp_point_t[]){{x, y}, {x + w, y}, {x + w, y + h}, {x, y + h}}, 4);
102-
// x += t; y += t; w -= 2 * t; h -= 2 * t;
103-
// pp_path_add_points(inner, (pp_point_t[]){{x, y}, {x + w, y}, {x + w, y + h}, {x, y + h}}, 4);
104-
// return poly;
105-
// }
106-
/*
107-
pp_poly_t* f_reg(PP_COORD_TYPE x, PP_COORD_TYPE y, PP_COORD_TYPE r, int s) {
108-
pp_poly_t *poly = pp_poly_new();
109-
pp_path_t *path = pp_poly_add_path(poly);
110-
for(int i = 0; i < s; i++) {
111-
pp_point_t p;
112-
float step = ((M_PI * 2.0f) / (float)s) * (float)i;
113-
p.x = sin(step) * r + x;
114-
p.y = cos(step) * r + y;
115-
pp_path_add_point(path, p);
116-
}
117-
return poly;
118-
}
119-
120-
pp_poly_t* reg(PP_COORD_TYPE x, PP_COORD_TYPE y, PP_COORD_TYPE r, int s, PP_COORD_TYPE t) {
121-
pp_poly_t *outer = f_reg(x, y, r, s);
122-
pp_poly_t *inner = f_reg(x, y, r - t, s);
123-
outer->paths->next = inner->paths;
124-
inner->paths = NULL;
125-
free(inner);
126-
return outer;
127-
}*/
12832
/*
129-
pp_poly_t* f_circ(PP_COORD_TYPE x, PP_COORD_TYPE y, PP_COORD_TYPE r) {
130-
int s = _pp_max(8, r);
131-
return f_reg(x, y, r, s);
132-
}
133-
134-
pp_poly_t* circ(PP_COORD_TYPE x, PP_COORD_TYPE y, PP_COORD_TYPE r, PP_COORD_TYPE t) {
135-
pp_poly_t *outer = f_circ(x, y, r);
136-
pp_poly_t *inner = f_circ(x, y, r - t);
137-
outer->paths->next = inner->paths;
138-
inner->paths = NULL;
139-
free(inner);
140-
return outer;
141-
}*/
142-
143-
pp_poly_t* f_star(PP_COORD_TYPE x, PP_COORD_TYPE y, int c, PP_COORD_TYPE or, PP_COORD_TYPE ir) {
144-
pp_poly_t *poly = pp_poly_new();
145-
pp_path_t *path = pp_poly_add_path(poly);
146-
for(int i = 0; i < c * 2; i++) {
147-
pp_point_t p;
148-
float step = ((M_PI * 2) / (float)(c * 2)) * (float)i;
149-
PP_COORD_TYPE r = i % 2 == 0 ? or : ir;
150-
p.x = sin(step) * r + x;
151-
p.y = cos(step) * r + y;
152-
pp_path_add_point(path, p);
153-
}
154-
return poly;
155-
}
156-
157-
pp_poly_t* star(PP_COORD_TYPE x, PP_COORD_TYPE y, int c, PP_COORD_TYPE or, PP_COORD_TYPE ir, PP_COORD_TYPE t) {
158-
pp_poly_t *outer = f_star(x, y, c, or, ir);
159-
pp_poly_t *inner = f_star(x, y, c, or - (t * or / ir), ir - t);
160-
outer->paths->next = inner->paths;
161-
inner->paths = NULL;
162-
free(inner);
163-
return outer;
164-
}
165-
16633
pp_poly_t* f_gear(PP_COORD_TYPE x, PP_COORD_TYPE y, int c, PP_COORD_TYPE or, PP_COORD_TYPE ir) {
16734
pp_poly_t *poly = pp_poly_new();
16835
pp_path_t *path = pp_poly_add_path(poly);
@@ -181,7 +48,7 @@ pp_poly_t* f_gear(PP_COORD_TYPE x, PP_COORD_TYPE y, int c, PP_COORD_TYPE or, PP_
18148
pp_path_add_point(path, p);
18249
}
18350
return poly;
184-
}
51+
}*/
18552

18653
pp_poly_t* gear(PP_COORD_TYPE x, PP_COORD_TYPE y, int c, PP_COORD_TYPE or, PP_COORD_TYPE ir, PP_COORD_TYPE t) {
18754
/*pp_poly_t *outer = f_gear(x, y, c, or, ir);
@@ -193,69 +60,6 @@ pp_poly_t* gear(PP_COORD_TYPE x, PP_COORD_TYPE y, int c, PP_COORD_TYPE or, PP_CO
19360
return NULL;
19461
}
19562

196-
pp_poly_t* line(PP_COORD_TYPE x1, PP_COORD_TYPE y1, PP_COORD_TYPE x2, PP_COORD_TYPE y2, PP_COORD_TYPE t) {
197-
pp_poly_t *poly = pp_poly_new();
198-
pp_path_t *path = pp_poly_add_path(poly);
199-
200-
// create a normalised perpendicular vector
201-
pp_point_t v = {y2 - y1, x2 - x1};
202-
float mag = sqrt(v.x * v.x + v.y * v.y);
203-
t /= 2.0f; v.x /= mag; v.y /= mag; v.x *= -t; v.y *= t;
204-
pp_path_add_points(path, (pp_point_t[]){{x1 + v.x, y1 + v.y}, {x2 + v.x, y2 + v.y}, {x2 - v.x, y2 - v.y}, {x1 - v.x, y1 - v.y}}, 4);
205-
return poly;
206-
}
207-
208-
/*
209-
pp_poly_t *pie(PP_COORD_TYPE x, PP_COORD_TYPE y, PP_COORD_TYPE r, float sa, float ea) {
210-
pp_poly_t *poly = pp_poly_new();
211-
pp_path_t *path = pp_poly_add_path(poly);
212-
pp_path_add_point(path, (pp_point_t){x, y});
213-
214-
sa = sa * (M_PI / 180.0f);
215-
ea = ea * (M_PI / 180.0f);
216-
int s = _pp_max(8, r);
217-
float astep = (ea - sa) / s;
218-
for(float a = sa; a < ea; a+=astep) {
219-
pp_point_t p;
220-
p.x = sin(a) * r + x;
221-
p.y = cos(a) * r + y;
222-
pp_path_add_point(path, p);
223-
}
224-
225-
return poly;
226-
}
227-
228-
229-
pp_poly_t *arc(PP_COORD_TYPE x, PP_COORD_TYPE y, PP_COORD_TYPE r, float sa, float ea, PP_COORD_TYPE thickness) {
230-
pp_poly_t *poly = pp_poly_new();
231-
pp_path_t *path = pp_poly_add_path(poly);
232-
233-
sa = sa * (M_PI / 180.0f);
234-
ea = ea * (M_PI / 180.0f);
235-
236-
int s = _pp_max(8, r);
237-
float astep = (ea - sa) / s;
238-
float a = sa;
239-
for(int i = 0; i <= s; i++) {
240-
pp_point_t p;
241-
p.x = sin(a) * r + x;
242-
p.y = cos(a) * r + y;
243-
pp_path_add_point(path, p);
244-
a += astep;
245-
}
246-
247-
r -= thickness;
248-
a = ea;
249-
for(int i = 0; i <= s; i++) {
250-
pp_point_t p;
251-
p.x = sin(a) * r + x;
252-
p.y = cos(a) * r + y;
253-
pp_path_add_point(path, p);
254-
a -= astep;
255-
}
256-
257-
return poly;
258-
}*/
25963

26064
int main() {
26165
pp_tile_callback(blend_tile);
@@ -302,10 +106,12 @@ int main() {
302106
poly = ppp_regular(r);
303107
}break;
304108
case 6: {
305-
poly = f_star(0, 0, 7, size, size * .75);
109+
ppp_star_def r = {0, 0, 7, size, size * 0.75f};
110+
poly = ppp_star(r);
306111
}break;
307112
case 7: {
308-
poly = star(0, 0, 7, size, size * .75, thickness);
113+
ppp_star_def r = {0, 0, 7, size, size * 0.75f, thickness};
114+
poly = ppp_star(r);
309115
}break;
310116
case 8: {
311117
ppp_rect_def r = {-size, -size, size * 2, size * 2, 0, size * .5, size * .05, size * .15, size * .7};

pretty-poly-primitives.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,19 @@ typedef struct {
6161
PP_COORD_TYPE f, t; // angle from and to
6262
} ppp_arc_def;
6363

64+
typedef struct {
65+
PP_COORD_TYPE x, y; // coordinates
66+
int c; // number of points on star
67+
PP_COORD_TYPE or, ir; // outer and inner radius for points
68+
PP_COORD_TYPE s; // stroke thickness (0 == filled)
69+
} ppp_star_def;
70+
71+
typedef struct {
72+
PP_COORD_TYPE x1, y1; // start point
73+
PP_COORD_TYPE x2, y2; // end point
74+
PP_COORD_TYPE s; // thickness
75+
} ppp_line_def;
76+
6477
pp_poly_t* ppp_rect(ppp_rect_def d);
6578
pp_poly_t* ppp_regular(ppp_regular_def d);
6679
pp_poly_t* ppp_circle(ppp_circle_def d);
@@ -174,6 +187,40 @@ pp_poly_t* ppp_arc(ppp_arc_def d) {
174187
return poly;
175188
}
176189

190+
pp_poly_t* ppp_star(ppp_star_def d) {
191+
pp_poly_t *poly = pp_poly_new();
192+
pp_path_t *path = pp_poly_add_path(poly);
193+
pp_path_t *inner = d.s != 0.0f ? pp_poly_add_path(poly) : NULL;
194+
195+
for(int i = 0; i < d.c * 2; i++) {
196+
float step = ((M_PI * 2) / (float)(d.c * 2)) * (float)i;
197+
PP_COORD_TYPE r = i % 2 == 0 ? d.or : d.ir;
198+
pp_path_add_point(path, (pp_point_t){sin(step) * r + d.x, cos(step) * r + d.y});
199+
200+
if(inner) { // append the inner path
201+
PP_COORD_TYPE ior = d.or - (d.s * d.or / d.ir);
202+
PP_COORD_TYPE iir = d.ir - d.s;
203+
PP_COORD_TYPE ir = i % 2 == 0 ? ior : iir;
204+
pp_path_add_point(inner, (pp_point_t){sin(step) * ir + d.x, cos(step) * ir + d.y});
205+
}
206+
}
207+
208+
return poly;
209+
}
210+
211+
pp_poly_t* ppp_line(ppp_line_def d) {
212+
pp_poly_t *poly = pp_poly_new();
213+
pp_path_t *path = pp_poly_add_path(poly);
214+
215+
// create a normalised perpendicular vector
216+
pp_point_t v = {d.y2 - d.y1, d.x2 - d.x1};
217+
float mag = sqrt(v.x * v.x + v.y * v.y);
218+
v.x /= mag; v.y /= mag; v.x *= -(d.s / 2.0f); v.y *= (d.s / 2.0f);
219+
pp_path_add_points(path, (pp_point_t[]){{d.x1 + v.x, d.y1 + v.y}, {d.x2 + v.x, d.y2 + v.y}, {d.x2 - v.x, d.y2 - v.y}, {d.x1 - v.x, d.y1 - v.y}}, 4);
220+
221+
return poly;
222+
}
223+
177224
#endif // PPP_IMPLEMENTATION
178225

179226
#endif // PPP_INCLUDE_H

pretty-poly.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ void pp_render(pp_poly_t *polygon);
146146

147147
#ifndef PP_MALLOC
148148
#define PP_MALLOC(size) malloc(size)
149+
#define PP_CALLOC(num, size) calloc(num, size)
149150
#define PP_REALLOC(p, size) realloc(p, size)
150151
#define PP_FREE(p) free(p)
151152
#endif

0 commit comments

Comments
 (0)