#+TITLE: Python CAD Tutorial 05 - Drawing lines in 3D space
#+DATE: 2013-10-05 12:00:00 UTC
#+DESCRIPTION: Connect our points from a previous tutorial together with lines.
#+FILETAGS: GTK-3:python:cad:opengl
#+CATEGORY: python
#+SLUG: 05-cad-drawing-lines-in-3d-space
#+THUMBNAIL: ../../../images/cad/tut05-interface.png
#+BEGIN_COMMENT
.. title:  Python CAD Tutorial 05 - Drawing lines in 3D space
.. slug: 05-cad-drawing-lines-in-3d-space.org
.. date: 2013-10-05 12:00:00 UTC
.. tags: GTK-3, python, cad, opengl
.. category: python
.. description: Connect our points from a previous tutorial together with lines.
.. type: text
#+END_COMMENT

[[https://code.launchpad.net/~oly/fabricad/tut05][View/Download Code]]


#+CAPTION: Lines
[[../../../images/cad/tut05-interface.png]]

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.

#+BEGIN_SRC python
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()
#+END_SRC

