- PyOpenGL
- numpy
Source Code :
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
import numpy as np
def init():
glClearColor(0.0, 0.0, 0.0, 0.0)
gluOrtho2D(-2.0, 2.0, -2.0, 2.0)
def heart_spiral_object():
glBegin(GL_LINE_STRIP)
glColor3f(0.7, 0.0, 0.0)
x = -1.140
while(x <= 1.140):
delta = np.cbrt(x*x) * np.sqrt(x*x) - 4*x*x + 4
y1 = (np.cbrt(x*x) + np.sqrt(delta)) / 2
y2 = (np.cbrt(x*x) - np.sqrt(delta)) / 2
glVertex2f(x, y1)
glVertex2f(x, y2)
x += 0.001
glEnd()
def plotpoints():
glClear(GL_COLOR_BUFFER_BIT)
glColor3f(1, 1.0, 1.0)
glPointSize(15)
glBegin(GL_LINES)
glVertex2f(-500, 0)
glVertex2f(500, 0)
glVertex2f(0, -500)
glVertex2f(0, 500)
glEnd()
heart_spiral_object()
glFlush()
def main():
glutInit(sys.argv)
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB)
glutInitWindowSize(500, 500)
glutInitWindowPosition(100, 100)
glutCreateWindow("Pertemuan 5 - Spiral Heart")
glutDisplayFunc(plotpoints)
init()
glutMainLoop()
main()
Output :