-
Notifications
You must be signed in to change notification settings - Fork 0
/
shapes.py
189 lines (167 loc) · 5.63 KB
/
shapes.py
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
# coding=utf-8
from math import *
import numpy as np
import matplotlib.pyplot as plt
from basic_shapes import Shape
# Sides must be an even number
# The number of triangles is sides/2 - 2
def createColorCircle(r, g, b, radius=1):
sides = 32
steps = pi / sides
t = np.arange(-np.pi, np.pi, steps)
x = radius * np.sin(t)
y = radius * np.cos(t)
# Defining locations and colors for each vertex of the shape
vertices = []
for i in range(len(x)):
# Positions
vertices.append(x[i])
vertices.append(y[i])
vertices.append(0.0) # 2D
# Colors
vertices.append(r)
vertices.append(g)
vertices.append(b)
# Defining connections among vertices
indices = []
nodos = len(x)
for i in range(nodos):
if (i != 0) and (i != (nodos - 1)):
indices.append(0)
indices.append(i)
indices.append(i + 1)
return Shape(vertices, indices)
# Sides must be an even number
# The number of triangles is sides/2 - 2
def createColorCircumference(r, g, b, radius=1):
sides = 10
steps = 0.01
t = np.arange(-np.pi, np.pi, steps)
x = radius * np.sin(t)
y = radius * np.cos(t)
radius2 = 0.99
x2 = radius2 * np.sin(t)
y2 = radius2 * np.cos(t)
# Defining locations and colors for each vertex of the shape
vertices = []
for i in range(len(x)):
# Outer circle
# Positions
vertices.append(x[i])
vertices.append(y[i])
vertices.append(0.0) # 2D
# Colors
vertices.append(r)
vertices.append(g)
vertices.append(b)
for i in range(len(x)):
# Inner circle
# Positions
vertices.append(x2[i])
vertices.append(y2[i])
vertices.append(0.0) # 2D
# Colors
vertices.append(r)
vertices.append(g)
vertices.append(b)
# Defining connections among vertices
indices = []
nodos = len(x)
for i in range(nodos):
if i < nodos - 1:
indices.append(i)
indices.append(i + 1)
indices.append(i + 629)
indices.append(i + 629)
indices.append(i + 629 + 1)
indices.append(i + 1)
return Shape(vertices, indices)
def createGradientQuad(r1, g1, b1, r2, g2, b2):
# Defining locations and colors for each vertex of the shape
vertices = [
# positions colors
-0.5, -0.5, 0.0, r1, g1, b1,
0.5, -0.5, 0.0, r1, g1, b1,
0.5, 0.5, 0.0, r2, g2, b2,
-0.5, 0.5, 0.0, r2, g2, b2]
# Defining connections among vertices
# We have a triangle every 3 indices specified
indices = [
0, 1, 2,
2, 3, 0]
return Shape(vertices, indices)
# The number of triangles is sides/2 - 2
def createColorSphere(r, g, b, sides=32):
t = np.linspace(-np.pi, np.pi, sides+1)
z = np.linspace(-1, 1, sides+1)
angle = np.linspace(0, np.pi, sides+1)
radius = np.sin(angle)
# Defining locations and colors for each vertex of the shape
vertices = []
for j in range(sides + 1):
if j == 0 or j == sides:
vertices += [0, 0, z[j]]
vertices += [r, g, b]
else:
x = radius[j] * np.sin(t)
y = radius[j] * np.cos(t)
for i in range(sides):
vertices += [x[i], y[i], z[j]] # Positions
vertices += [r, g, b] # Colors
# Defining connections among vertices
indices = []
nodes = len(vertices)//6
#for i in range(nodes):
# if i == 0: # Triangulos inferiores
# #for j in range(i+1, sides):
# # indices += [i, j, (j+1) % (sides+1)]
# #indices += [i, sides, 1]
# continue
# elif i == nodes - 1: # Triangulos superiores
# #for j in range(i-sides, i-1):
# # indices += [i, j, j+1]
# #indices += [i, i-1, i-sides]
# continue
# elif i <= 4:
# indices += [i, i+1, i+sides]
# indices += [i+1, i+sides, i+1+sides]
# Cheking every level
for i in range(sides):
print(f"level = {i}")
if i == 0: # Triangulos inferiores
print("Case 1")
for j in range(i+1, sides):
indices += [i, j, (j+1) % (sides+1)]
print(f"{i} {j} {(j+1) % (sides+1)}")
indices += [i, sides, 1]
print(f"{i} {sides} {1}")
elif i == sides - 1: # Triangulos superiores
print("Case 2")
for j in range(0, sides):
k = (sides - 1) * i + j
if j == sides - 1:
indices += [nodes - 1, k, (sides - 1) * i]
print(f"{nodes - 1} {k} {(sides - 1) * i}")
else:
indices += [nodes - 1, k, k+1]
print(f"{nodes - 1} {k} {k+1}")
else:
print("Case 3")
for j in range(1, sides+1):
k = (i - 1) * sides + j
if j == sides + 1:
indices += [k, k + 1, k + sides]
print(f"{k} {k + 1} {k + sides}")
indices += [k + 1, (i - 1) * sides + 1, k + 1 + sides]
print(f"{k + 1} {(i - 1) * sides + 1} {k + 1 + sides}")
else:
indices += [k, k+1, k+sides]
print(f"{k} {k+1} {k + sides}")
indices += [k+1, k+sides, k+1+sides]
print(f"{k+1} {k + sides} {k+1+sides}")
#for i in range(0, len(indices), 3):
# print(f"{indices[i]} {indices[i+1]} {indices[i+2]}")
#print(X)
#print(Y)
#print(Z)
return Shape(vertices, indices)