File: create-ufo-obv.c

package info (click to toggle)
acm 6.0%2B20200416-1.2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,972 kB
  • sloc: ansic: 49,164; tcl: 941; makefile: 644; sh: 594
file content (125 lines) | stat: -rw-r--r-- 2,586 bytes parent folder | download | duplicates (3)
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
/**
 * Creates the U.F.O. object in obv format as required by our Vlib.
 * Basically a cone with 8 round "shields" (or laser beam thrower, or whatever
 * they are) all around, 4 red to the right, 4 green to the left.
 * Sends its output to stdout, so one has to redirect it to
 * ../objects/aircraft/ufo.obv by hand.
 * 
 * @file
 * @author Umberto Salsi <salsi@icosaedro.it>
 * @version $Date: 2017/10/30 02:17:57 $
 */

/*
 * LINKER_OPTIONS -lm
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#include "../src/V/Vlibmath.h"
//#include "../src/V/VObjects.h"

// Height (ft):
#define H (20.0)

// Base radius:
#define R (1.1*H)

// Shields' radius:
#define S (0.25*H)

static int no_of_points;
static int no_of_polygons;
static char this_polygon_color[100];
static int this_polygon_no_vertices;
static char this_polygon_vertices[2000];
static char points[2000];
static char polygons[2000];


static void openPolygon(char *colors)
{
	strcpy(this_polygon_color, colors);
	this_polygon_no_vertices = 0;
	strcpy(this_polygon_vertices, "");
}


static void addPoint(VPoint *p)
{
	char s[100];
	
	no_of_points++;
	
	sprintf(s, "%d %.2f %.2f %.2f\n", no_of_points, p->x, p->y, p->z);
	strcat(points, s);
	
	sprintf(s, " %d", no_of_points);
	strcat(this_polygon_vertices, s);
	this_polygon_no_vertices++;
}


static void closePolygon()
{
	char s[1000];
	sprintf(s, "%s %d%s\n", this_polygon_color, this_polygon_no_vertices,
		this_polygon_vertices);
	strcat(polygons, s);
	no_of_polygons++;
}


static void writeAll()
{
	printf("%d %d\n", no_of_points, no_of_polygons);
	printf("%s", points);
	printf("%s", polygons);
}


int main(int argc, char** argv)
{
	int i, j;
	double da;
	VPoint shield[6];
	
	printf("ufo\n");
	
	// The fuselage is a cone with vertex (0,0,-H) and base on the xy plane:
	da = 2 * M_PI / 8;
	for(i = 0; i < 8; i++){
		openPolygon("(gray44 clip)");
		addPoint(&(VPoint){R*cos(i*da+da), R*sin(i*da+da), 0});
		addPoint(&(VPoint) {0, 0, -H});
		addPoint(&(VPoint){R*cos(i*da), R*sin(i*da), 0});
		closePolygon();
	}
	
	// Vertices of an hexagonal shield located along x:
	for(i = 0; i < 6; i++){
		double a = - 2 * M_PI / 6 * i;
		shield[i] = (VPoint) {1.1*R, S*cos(a), S*sin(a)};
	}
	
	// Place 8 shields around the base, right red, left green:
	for(i = 0; i < 8; i++){
		VMatrix m;
		VPoint q;
		VIdentMatrix(&m);
		VRotate(&m, 3, 2*M_PI/16 + i * da);
		openPolygon(i < 4? "(red clip)" : "(green clip)");
		for(j = 0; j < 6; j++){
			VTransform(&shield[j], &m, &q);
			addPoint(&q);
		}
		closePolygon();
	}
	
	writeAll();

	return 0;
}