|
|
1.1 root 1: #include <stdio.h>
2:
3: #define SZ 512
4:
5: typedef struct Node {
6: int x, y;
7: struct Node *nxt; /* contour */
8: int soc; /* node is start of new curve */
9: } Node;
10:
11: Node *frst = (Node *) 0;
12: int lcnt=0;
13: extern int Factor;
14:
15: zaplist()
16: { Node *ptr = frst;
17: Node *save;
18:
19: while (ptr)
20: { save = ptr->nxt;
21: free(ptr);
22: ptr = save;
23: }
24: frst = (Node *) 0;
25: }
26:
27: addnode(n, m, p)
28: { Node *tmp, *hunt, *last;
29:
30: tmp = (Node *) malloc(sizeof(Node));
31: if (!tmp)
32: return;
33:
34: tmp->nxt = (Node *) 0;
35: tmp->x = n;
36: tmp->y = m;
37: tmp->soc = p;
38: if (!frst)
39: frst = tmp;
40: else
41: { last = frst; hunt = last->nxt;
42: while (hunt)
43: { last = hunt;
44: hunt = last->nxt;
45: }
46: last->nxt = tmp;
47: }
48: }
49:
50: compar(a, b)
51: int *a, *b;
52: {
53: return (*a>*b)?1:(*a<*b)?-1:0;
54: }
55:
56: runcontour()
57: { Node *hunt;
58: int miny, maxy;
59: int *xx;
60: int x, y, cnt, total, partial;
61:
62: if (!frst)
63: return;
64:
65: hunt = frst;
66: miny = hunt->y; maxy = hunt->y;
67: cnt = 0;
68: while (hunt)
69: { if (hunt->y < miny) miny = hunt->y;
70: if (hunt->y > maxy) maxy = hunt->y;
71: hunt = hunt->nxt;
72: cnt++;
73: }
74: xx = (int *) malloc(cnt * sizeof(int)); /* max # intersections */
75: if (!xx)
76: { fprintf (stderr, "out of memory\n");
77: return;
78: }
79: for (y = miny; y < maxy; y++)
80: { if ((cnt = intersect(xx, y)) <= 1)
81: continue;
82: qsort(xx, cnt, sizeof(int), compar);
83: for (x = total = 0; x < cnt-1; x += 2)
84: total += xx[x+1] - xx[x];
85: for (x = partial = 0; x < cnt-1; x += 2)
86: { doline(xx[x], xx[x+1]+1, SZ-y, partial, total);
87: partial += xx[x+1] - xx[x];
88: } }
89: free(xx);
90: }
91:
92: doline(a, b, c, p, t)
93: { int q;
94:
95: if (a >= b) return;
96: lcnt++;
97: if (lcnt%100 == 0)
98: printf("}\ndef curve%d() {\n", lcnt);
99: if (b-a == t)
100: printf("mapper(%d,%d,0,%d,%d)\n", a, b, SZ-1, c);
101: else
102: { if (p == 0)
103: { printf("setter(0, %d, %d, %d)\n", a, c, c*255/(SZ-1));
104: printf("mapper(%d,%d,0,(%d*%d)/%d,%d)\n",a,b,SZ-1,b-a,t,c);
105: } else
106: { printf("x=(%d*%d)/%d;\n", SZ-1, p, t);
107: printf("y=x+(%d*%d)/%d;\n", SZ-1, (b-a), t);
108: printf("mapper(%d,%d,x,y,%d)\n", a, b, c);
109: if (p+b-a == t)
110: printf("setter(%d, %d, %d, %d)\n",b,SZ-1,c,c*255/(SZ-1));
111: }
112: }
113: }
114:
115: #define Sign(n) ((n>0)?1:0)
116:
117: intersect(xx, y)
118: int *xx;
119: {
120: Node *hunt, *last;
121: int i, j, n, m;
122: int nhit=0;
123:
124: last = frst;
125: hunt = last->nxt;
126: while (hunt)
127: { n = last->y - y;
128: m = hunt->y - y;
129: if (Sign(n) != Sign(m))
130: { i = last->x; j = hunt->x;
131: if (hunt->x > last->x)
132: xx[nhit++] = i + (j-i)*n/(n-m);
133: else
134: xx[nhit++] = i - (i-j)*n/(n-m);
135: } /* else if (n == 0 && m == 0)
136: { if (hunt->x > last->x)
137: doline(last->x, hunt->x, SZ-y);
138: else
139: doline(hunt->x, last->x, SZ-y);
140: } */
141: last = hunt;
142: hunt = last->nxt;
143: if (hunt && hunt->soc)
144: { last = hunt;
145: hunt = last->nxt;
146: }
147: }
148: return nhit;
149: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.