-
Notifications
You must be signed in to change notification settings - Fork 0
/
bsplines.c
188 lines (143 loc) · 3.97 KB
/
bsplines.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
#include "FPToolkit.c"
#include <assert.h>
// Hard-coded coefficients, scary!
const double a_coef[4] = { 1.0f/6, -1.0f/2, 1.0f/2, -1.0f/6 };
const double b_coef[4] = { 2.0f/3, 0.0, -1.0f, 1.0f/2 };
const double c_coef[4] = { 1.0f/6, 1.0f/2, 1.0f/2, -1.0f/2 };
const double d_coef[4] = { 0.0f, 0.0f, 0.0f, 1.0f/6 };
/* Returns remainder of synthetic division. Accepts NULL argument for `quot`.
* Copied from Homework 2.
*/
double syndiv(int degree, double num[], double denom, double quot[])
{
if (degree <= 0)
return num[0];
if (quot) {
quot[0] = syndiv(degree - 1, num + 1, denom, quot + 1);
return num[0] + denom * quot[0];
} else
return num[0] + denom * syndiv(degree - 1, num + 1, denom, NULL);
}
double ** new_spline(int n_segments, int degree)
{
double ** rows = malloc(sizeof(double*) * n_segments);
for (int i = 0; i < n_segments; ++i)
rows[i] = malloc(sizeof(double) * (degree + 1));
return rows;
}
void del_spline(int n_segments, double **s)
{
for (int i = 0; i < n_segments; ++i)
free(s[i]);
free(s);
}
void add_scaled_f(int deg, double *accum, const double *f, double c)
{
for (int i = 0; i <= deg; i++)
accum[i] += f[i] * c;
}
void cubic_seg(int i_seg, double *seg, int n_pts, double *pts)
{
assert(i_seg + 3 < n_pts);
const double *funcs[] = {a_coef, b_coef, c_coef, d_coef};
for (int i = 0; i < 4; i++)
seg[i] = 0;
for (int i = 0; i < 4; i++)
add_scaled_f(3, seg, funcs[i], pts[i_seg + i]);
}
void cubic_spline(double **s, int n_pts, double *pts)
{
assert(n_pts > 3);
for (int i = 0; i < n_pts - 3; i++)
cubic_seg(i, s[i], n_pts, pts);
}
void G_spline_seg(int degree, double *segx, double *segy)
{
double dt = 1.0f/1024.0f;
double x, y;
double x_old = syndiv(degree, segx, 0, NULL);
double y_old = syndiv(degree, segy, 0, NULL);
G_rgb(1.0f, 0.0f, 0.0f); // red
for (double t = 0; t <= 1.0f; t += dt) {
x = syndiv(degree, segx, t, NULL);
y = syndiv(degree, segy, t, NULL);
G_line(x_old, y_old, x, y);
x_old = x;
y_old = y;
}
}
void G_cubic_spline(int n_segments, double **sx, double **sy)
{
for (int i = 0; i < n_segments; i++)
G_spline_seg(3, sx[i], sy[i]);
}
void pts_to_cubic_spline(int n_pts, double *xpts, double *ypts)
{
double **sx = new_spline(n_pts - 3, 3);
double **sy = new_spline(n_pts - 3, 3);
cubic_spline(sx, n_pts, xpts);
cubic_spline(sy, n_pts, ypts);
G_cubic_spline(n_pts - 3, sx, sy);
del_spline(n_pts - 3, sx);
del_spline(n_pts - 3, sy);
}
int main(int argc, char **argv) {
if (argc != 2) {
printf("%s: exactly 1 argument required\n", argv[0]);
return 0;
}
int mode = argv[1][0] - '0';
if ( (mode != 0 && mode != 1) || strlen(argv[1]) != 1 ) {
printf("%s: %s is not 0 or 1\n", argv[0], argv[1]);
return 0;
}
const int MAX_PTS = 100;
double x[MAX_PTS];
double y[MAX_PTS];
double f[MAX_PTS];
double stripwidth = 10;
double boxx = 800;
double boxy = 800;
double swidth = boxx + stripwidth;
double sheight = boxy;
double radius = 2;
int i = 0;
double p[2] = {0, 0};
int maxi = MAX_PTS;
// BACKGROUND
G_init_graphics (swidth,sheight) ; // interactive graphics
G_rgb (0.0, 0.0, 0.0) ; // black
G_clear ();
// DRAW STRIP BOUNDARY
G_rgb (0.0, 0.0, 1.0) ; // blue
G_line (boxx, 0, boxx, sheight-1);
// GET POINTS
if (mode == 1) {
scanf("%d", &maxi);
}
for (i = 0; i < MAX_PTS && i < maxi; i++) {
if (mode == 0) {
G_wait_click(p);
if (p[0] > boxx)
break;
} else {
scanf("%lf %lf", &p[0], &p[1]);
}
x[i] = p[0];
y[i] = p[1];
// DRAW CIRCLES
G_rgb(1.0, 0.0, 0.0) ; // red
G_fill_circle(p[0], p[1], radius);
}
// DRAW PARABOLA OF BEST FIT
// quad_reg(i, x, y, 0, boxx);
// DRAW CUBIC SPLINE
if (i > 3)
pts_to_cubic_spline(i, x, y);
else
printf("not enough points\n");
int key ;
key = G_wait_key() ; // pause so user can see results
G_save_image_to_file("demo.xwd");
return 0;
}