Projects for CG Class

project2/libgraphics.py


#!/usr/bin/env python

import math

from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *

class D3point () :
    def __init__ (self, x = 0, y = 0, z = 0) :
        self.x = x
        self.y = y
        self.z = z

class D2point () :
    def __init__ (self, x = 0, y = 0) :
        self.x = x
        self.y = y

def oval(a, b, cut, start_deg = 0.0, end_deg = 2*math.pi) :
    list = []
    angle = start_deg
    for n in range(cut) :
        list.append(D2point(math.cos(angle)*a, math.sin(angle)*b))
        angle = angle + (end_deg-start_deg)/cut
    return list

def circle(r, cut, start_deg = 0.0, end_deg = 2*math.pi) :
    return oval(r, r, cut, start_deg, end_deg)

def D2rotateArray(array, deg) :
    return [
        D2point(
            point.x * math.cos(deg) + point.y * math.sin(deg),
            point.y * math.cos(deg) - point.x * math.sin(deg)
        )
        for point in array
    ]

def linedPoly (array, r, g, b, width = 10) :
    glLineWidth(width)
    glBegin(GL_POLYGON)
    glColor3f(r, g, b)
    for point in array :
        glVertex3f(point.x, point.y, 0.0)
    glEnd()

    glTranslate(0.0, 0.0, 0.0001)
    glBegin(GL_LINE_LOOP)
    glColor3f(0.0, 0.0, 0.0)
    for point in array :
        glVertex3f(point.x, point.y, 0.0)
    glEnd()

def lineLoopArray (array, r,g,b, width = 1) :
    glLineWidth(width)
    glBegin(GL_LINE_LOOP)
    glColor3f(r, g, b)
    for point in array :
        glVertex3f(point.x, point.y, 0.0)
    glEnd()
    glLineWidth(1)

One Response

Subscribe to comments with RSS.

  1. […] https://bizkit4cg.wordpress.com/project2libgraphicspy/ « CG Class Project 1: 2D cartoon figure […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: