Figure 1: Lines
In this tutorial we will create a new line class. Initially this will be setup to create an unlimited number of lines, drawn on every second click of the mouse.Our new class will be similar to the point class, but will contain two points to store the start and stop coordinates of the lines. We will also adjust the draw method to draw lines instead of points.
from point import createpoint class createline: p1 = p2 = None color = 0, 1, 0 display_color = (0, 1, 0) def __init__(self, point1, point2): """ create the start and stop points and colour of the line""" self.p1 = createpoint(point1) self.p2 = createpoint(point2) def glvertex(self): """ Opengl vertex useful so we can dont have to glbegin and glend for each point""" glVertex3f(self.p1.x, self.p1.y, self.p1.z) glVertex3f(self.p2.x, self.p2.y, self.p2.z) def get_points(self): """ return the start and end point of the line could be used in a drawing loop""" for p in (self.p1, self.p2): yield (p) def draw(self): """ lets draw the line baase on the cordinates, also draw the points for now """ glColor3f(*self.display_color) glBegin(GL_LINES) glVertex3f(self.p1.x, self.p1.y, self.p1.z) glVertex3f(self.p2.x, self.p2.y, self.p2.z) glEnd() self.p1.draw() self.p2.draw()