-
Notifications
You must be signed in to change notification settings - Fork 4
/
pconic.c
195 lines (157 loc) · 4.7 KB
/
pconic.c
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
/* pconic --- plot Bezier curves */
/* Note: this program was originally written for a non-HPGL plotter,
the BMC B-1000. It uses a slightly different command language from HPGL,
although the commands are still two ASCII letters.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#include "hpgllib.h"
#define NPTS (16)
struct Weights {
double w0;
double w1;
double w2;
};
struct Point {
double x, y;
};
double quadfunc(const struct Weights *const wp, const double x0, const double x1, const double x2,
const double u);
void genconic(struct Point p0, struct Point p1, struct Point p2, const struct Weights *const wt, double x[], double y[], double w[], const int n);
void gencurve(const struct Weights *const wp, const double x0, const double x1, const double x2,
double x[], const int n);
void showcurve(const int colr, double xpts[], double ypts[], double wpts[], const int n);
int main(const int argc, char *const argv[])
{
int opt;
double maxx, maxy;
static struct Point p0 = {
180.0 * 40.0, 250.0 * 40.0
};
static struct Point p1 = {
0.0, 250.0 * 40.0
};
static struct Point p2 = {
0.0, 125.0 * 40.0
};
static struct Point p3 = {
0.0, 0.0
};
static struct Point p4 = {
180.0 * 40.0, 0.0
};
static struct Point p5 = {
360.0 * 40.0, 0.0
};
static struct Point p6 = {
360.0 * 40.0, 125.0 * 40.0
};
static struct Point p7 = {
360.0 * 40.0, 250.0 * 40.0
};
double xpts[NPTS + 1], ypts[NPTS + 1], wpts[NPTS + 1];
struct Weights wt;
int i;
while ((opt = getopt (argc, argv, "no:p:s:t:v:")) != -1) {
switch (opt) {
case 'n':
case 'o':
case 'p':
case 's':
case 't':
case 'v':
plotopt (opt, optarg);
break;
default: /* '?' */
fprintf (stderr, "Usage: %s [-p pen] [-s <size>] [-t title]\n", argv[0]);
fprintf (stderr, " <size> ::= A1 | A2 | A3 | A4 | A5\n");
exit (EXIT_FAILURE);
}
}
/* Select first pen and draw border */
if (plotbegin(1) < 0) {
fputs("Failed to initialise HPGL library\n", stderr);
exit(EXIT_FAILURE);
}
// printf ("IP1;PS3;\n");
getplotsize(&maxx, &maxy);
wt.w0 = 1.0;
wt.w1 = 3.0;
wt.w2 = 1.0;
for (i = 0; i < 10; i++) {
p1.x = (8.0 * 40.0) * i;
p2.x = p1.x;
p3.x = p1.x;
p5.x = (360.0 * 40.0) - ((8.0 * 40.0) * i);
p6.x = p5.x;
p7.x = p5.x;
p0.y = (250.0 * 40.0) - ((8.0 * 40.0) * i);
p1.y = p0.y;
p7.y = p0.y;
genconic(p0, p1, p2, &wt, xpts, ypts, wpts, NPTS);
showcurve(1, xpts, ypts, wpts, NPTS);
genconic(p2, p3, p4, &wt, xpts, ypts, wpts, NPTS);
showcurve(1, xpts, ypts, wpts, NPTS);
genconic(p4, p5, p6, &wt, xpts, ypts, wpts, NPTS);
showcurve(1, xpts, ypts, wpts, NPTS);
genconic(p6, p7, p0, &wt, xpts, ypts, wpts, NPTS);
showcurve(1, xpts, ypts, wpts, NPTS);
}
// printf("MA0,0;CH\n");
plotend();
return (0);
}
/* quadfunc --- rational quadratic function */
double quadfunc(const struct Weights *const wp, const double x0, const double x1, const double x2,
const double u)
{
double t0, t1, t2;
t0 = 1.0 - u;
t0 *= t0;
t0 *= x0 * wp->w0;
t1 = u * (1.0 - u);
t1 *= 2.0;
t1 *= x1 * wp->w1;
t2 = u * u;
t2 *= x2 * wp->w2;
return (t0 + t1 + t2);
}
/* gencurve --- generate 'n + 1' points on the curve */
void gencurve(const struct Weights *const wp, const double x0, const double x1, const double x2,
double x[], const int n)
{
const double step = 1.0 / (double)n; /* The increment in 't' for each step */
int i;
for (i = 0; i <= n; i++) {
const double t = i * step; /* The parameter, 0..1 */
x[i] = quadfunc(wp, x0, x1, x2, t);
}
}
/* genconic --- generate a conic section from 'p0' to 'p2' */
void genconic(struct Point p0, struct Point p1, struct Point p2, const struct Weights *const wt, double x[], double y[], double w[], const int n)
{
gencurve(wt, p0.x, p1.x, p2.x, x, n);
gencurve(wt, p0.y, p1.y, p2.y, y, n);
gencurve(wt, 1.0, 1.0, 1.0, w, n);
}
void showcurve(const int colr, double xpts[], double ypts[], double wpts[], const int n)
{
int i;
int x, y;
x = (xpts[0] / wpts[0]) + 0.5;
y = (ypts[0] / wpts[0]) + 0.5;
// printf("MA%d,%d\n", y, x);
moveto(x, y);
// printf("DA");
for (i = 1; i <= n; i++) {
x = (xpts[i] / wpts[i]) + 0.5;
y = (ypts[i] / wpts[i]) + 0.5;
// printf("%05d,%05d", x, y);
// if (i < n)
// printf(",");
lineto(x, y);
}
// printf("\n");
}