aboutsummaryrefslogtreecommitdiff
path: root/geometry/cylinder.py
blob: b576a62efad8abe6e07b1928a2c6c4bf1095d6c4 (plain)
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
import numpy

from OpenGL.GL import *

class Cylinder:
    def __init__(self, x, y, z, r, h = 0, l = 0):
        self.x = x
        self.y = y
        self.z = z
        self.r = r
        self.h = h
        self.l = l

        self.circle = []
        for i in range(33):
            alpha = 2 * numpy.pi * (i/32)
            c0 = self.r * numpy.cos(alpha)
            c1 = self.r * numpy.sin(alpha)
            self.circle.append((c0, c1))

    def indicator(self):
        if self.h == 0:
            return lambda x, y, z: (x - self.x)**2 + (z - self.z)**2 < self.r*self.r and y >= self.y and y <= self.y+self.l
        else:
            return lambda x, y, z: (x - self.x)**2 + (y - self.y)**2 < self.r*self.r and z >= self.z and z <= self.z+self.h

    def draw(self):
        glBegin(GL_TRIANGLE_FAN)

        if self.h == 0:
            glNormal(0,-1, 0)
            glVertex(self.x, self.y, self.z)
            for (c0, c1) in self.circle:
                glNormal(0, -1, 0)
                glVertex(self.x+c0, self.y, self.z+c1)
        else:
            glNormal(0, 0, -1)
            glVertex(self.x, self.y, self.z)
            for (c0, c1) in self.circle:
                glVertex(self.x+c0, self.y+c1, self.z)

        glEnd()

        glBegin(GL_TRIANGLE_FAN)
        if self.h == 0:
            glNormal(0, 1, 0)
            glVertex(self.x, self.y+self.l, self.z)
            for (c0, c1) in self.circle:
                glVertex(self.x+c0, self.y+self.l, self.z+c1)
        else:
            glNormal(0, 0, 1)
            glVertex(self.x, self.y, self.z+self.h)
            for (c0, c1) in self.circle:
                glVertex(self.x+c0, self.y+c1, self.z+self.h)

        glEnd()

        glBegin(GL_TRIANGLE_STRIP)

        if self.h == 0:
            for (c0, c1) in self.circle:
                glNormal(c0, 0, c1)
                glVertex(self.x+c0, self.y, self.z+c1)
                glVertex(self.x+c0, self.y + self.l, self.z+c1)
        else:
            for (c0, c1) in self.circle:
                glNormal(c0, c1, 0)
                glVertex(self.x+c0, self.y+c1, self.z)
                glVertex(self.x+c0, self.y+c1, self.z+self.h)

        glEnd()