Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

improve ShapeTransform.py

  • Loading branch information...
commit b0d323fef70e51e7ae93a613495a570d8585de46 1 parent 6a0486d
@jdf authored
Showing with 37 additions and 50 deletions.
  1. +37 −50 examples.py/3D/Form/ShapeTransform/ShapeTransform.py
View
87 examples.py/3D/Form/ShapeTransform/ShapeTransform.py
@@ -1,32 +1,32 @@
"""
- * Shape Transform
- * by Ira Greenberg.
- *
- * Illustrates the geometric relationship
- * between Cube, Pyramid, Cone and
- * Cylinder 3D primitives.
- *
- * Instructions:<br />
- * Up Arrow - increases points<br />
- * Down Arrow - decreases points<br />
- * 'p' key toggles between cube/pyramid<br />
- """
+ Shape Transform
+ by Ira Greenberg.
+ (Rewritten in Python by Jonathan Feinberg.)
+
+ Illustrates the geometric relationship
+ between Cube, Pyramid, Cone and
+ Cylinder 3D primitives.
-from processing.core import PVector
+ Instructions:
+ Up Arrow - increases points
+ Down Arrow - decreases points
+ 'p' key toggles between cube/pyramid
+ """
-pts = 4
+# constants
radius = 99
cylinderLength = 95
-isPyramid = False
angleInc = PI / 300.0
+# globals that can be chaned by the user
+pts = 12
+isPyramid = False
+
def setup():
size(640, 360, P3D)
noStroke()
-
+
def draw():
- global cylinderLength
-
background(170, 95, 95)
lights()
fill(255, 200, 200)
@@ -34,53 +34,40 @@ def draw():
rotateX(frameCount * angleInc)
rotateY(frameCount * angleInc)
rotateZ(frameCount * angleInc)
- # initialize vertex arrays
- vertices = [[PVector() for x in range(pts+1)],
- [PVector() for x in range(pts+1)]]
- # fill arrays
- for i in range(2):
- angle = 0
- for v in vertices[i]:
- if isPyramid:
- if i == 1:
- v.x = 0
- v.y = 0
- else:
- v.x = cos(radians(angle)) * radius
- v.y = sin(radians(angle)) * radius
- else:
- v.x = cos(radians(angle)) * radius
- v.y = sin(radians(angle)) * radius
- v.z = cylinderLength
- # the .0 after the 360 is critical
- angle += 360.0 / pts
- cylinderLength *= -1
-
+
+ dTheta = TWO_PI / pts
+ x = lambda(j): cos(dTheta * j) * radius
+ y = lambda(j): sin(dTheta * j) * radius
+
# draw cylinder tube
beginShape(QUAD_STRIP)
- for j in range(pts+1):
- vertex(vertices[0][j].x, vertices[0][j].y, vertices[0][j].z)
- vertex(vertices[1][j].x, vertices[1][j].y, vertices[1][j].z)
+ for j in range(pts + 1):
+ vertex(x(j), y(j), cylinderLength)
+ if isPyramid:
+ vertex(0, 0, -cylinderLength)
+ else:
+ vertex(x(j), y(j), -cylinderLength)
endShape()
#draw cylinder ends
- for i in range(2):
+ beginShape()
+ for j in range(pts + 1):
+ vertex(x(j), y(j), cylinderLength)
+ endShape(CLOSE)
+ if not isPyramid:
beginShape()
- for v in vertices[i]:
- vertex(v.x, v.y, v.z)
+ for j in range(pts + 1):
+ vertex(x(j), y(j), -cylinderLength)
endShape(CLOSE)
-
+
"""
up/down arrow keys control
polygon detail.
"""
def keyPressed():
global pts, isPyramid
- print key
if key == CODED:
- # pts
if keyCode == UP and pts < 90:
pts += 1
- print pts
elif keyCode == DOWN and pts > 4:
pts -= 1
elif key == ord('p'):
Please sign in to comment.
Something went wrong with that request. Please try again.