Uploaded by Prajwal Adhikari

comp 342 lab3

advertisement
1. Write a program to implement mid-point circle Drawing
Algorithm.
Source Code:
import pygame
from pygame.locals import *
from OpenGL.GL import *
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 800
def draw_pixel(xc, yc, x, y):
glBegin(GL_POINTS)
glColor3f(1.0, 1.0, 1.0)
glVertex2f(xc + x, yc + y)
glVertex2f(xc - x, yc + y)
glVertex2f(xc + x, yc - y)
glVertex2f(xc - x, yc - y)
glVertex2f(xc + y, yc + x)
glVertex2f(xc - y, yc + x)
glVertex2f(xc + y, yc - x)
glVertex2f(xc - y, yc - x)
glEnd()
def draw_circle(radius, xc, yc):
x = 0
y = radius
p = 1 - radius
pygame.display.set_mode((SCREEN_WIDTH,
SCREEN_HEIGHT), DOUBLEBUF | OPENGL)
glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(0, SCREEN_WIDTH, 0, SCREEN_HEIGHT, -1, 1)
glMatrixMode(GL_MODELVIEW)
draw_pixel(xc, yc, x, y)
while x < y:
x += 1
if p <= 0:
p = p + 2 * x + 1
else:
y -= 1
p = p + 2 * (x - y) + 1
draw_pixel(xc, yc, x, y)
pygame.display.flip()
draw_circle(100, 400, 400)
# Keeping the window open until manually closed
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
Output:
2. Write a Program to implement mid- point Ellipse Drawing
Algorithm.
Source Code:
Output:
Conclusion
In this lab assignment, I used two different types of algorithms such as mid
point circle drawing algorithm and mid point ellipse drawing algorithm.
This lab work was useful for me to learn the implementation of midpoint
ellipse drawing algorithm and midpoint circle drawing algorithms.
Download