Star
Star
//
#include "stdafx.h"
#include<Windows.h>
#include<glut.h>
#include<gl/GLU.h>
#include<math.h>
#include<fstream>
#define CIRCLE 1
#define TRIANGLE 2
#define SQUARE 3
#define STAR 4
#define PENTAGON 5
#define HEXAGON 6
#define HEPTAGON 7
#define OCTAGON 8
float angle=0.0,red=1.0,blue=1.0,green=1.0;
void myInit(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glColor3f(0.0f, 0.0f, 0.0f);
glPointSize(10.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,640.0,0.0,480.0);
}
class IntRect //aligned rectangle with integer coordinates, used for viewport
{
public:
IntRect() { l = 0; r = 100; b = 0; t = 100; }
IntRect(int left, int right, int bottom, int top)
{
l = left; r = right; b = bottom; t = top;
}
void set(int left, int right, int bottom, int top)
{
l = left; r = right; b = bottom; t = top;
}
void draw(void); //draw this rectangle using OpenGL
int getL(void)
{
return l;
}
int getR(void)
{
return r;
}
int getT(void)
{
return t;
}
int getB(void)
{
return b;
}
private:
int l, r, b, t;
};
class RealRect //simlar to IntRect but w/ floating points & used for world window
{
public:
RealRect() { l = 0; r = 100; b = 0; t = 100; }
RealRect(float left, float right, float bottom, float top)
{
l = left; r = right; b = bottom; t = top;
}
void set(float left, float right, float bottom, float top)
{
l = left; r = right; b = bottom; t = top;
}
float getL(void)
{
return l;
}
float getR(void)
{
return r;
}
float getT(void)
{
return t;
}
float getB(void)
{
return b;
}
void draw(void); //draw this rectangle using OpenGL
private:
float l, r, b, t;
};
//<<End Support Classes>>>
class Canvas
{
public:
Canvas(int width, int height, char* windowTitle); //constructor
void setWindow(float l, float r, float b, float t);
void setViewport(int l, int r, int b, int t);
IntRect getViewport(void); //divulge the viewport data
RealRect getWindow(void); // divulge the window data
float getWindowAspectRatio(void);
void clearScreen();
void setBackgroundColor(float r, float g, float b);
void setColor(float r, float g, float b);
void lineTo(float x, float y);
void lineTo(Point2 p);
void moveTo(float x, float y);
void moveTo(Point2 p);
void moveRel(float dx, float dy);
void turnTo(float angle);
void turn(float angle);
void forward(float dist, int isVisible);
Point2 Tween(Point2 A, Point2 B, float t);
void drawTween(Point2 A[], Point2 B[], int N, float t);
private:
Point2 CP; //current position in the world
float CD; //current direction in the world
IntRect viewport; //the current window
RealRect window; //the current viewport
};
//<<moveTo>>
//changes current point
void Canvas::moveTo(float x, float y)
{
CP.set(x, y);
}
//<<lineTo>>
//draws a line from current point to new point
void Canvas::lineTo(float x, float y)
{
glBegin(GL_LINES);
glVertex2f((GLfloat)CP.getX(), (GLfloat)CP.getY());
glVertex2f((GLfloat)x, (GLfloat)y); //draw the line
glEnd();
CP.set(x, y); //update current point to new point
glFlush();
}
//<<setWindow>>
void Canvas::setWindow(float l, float r, float b, float t)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D((GLdouble)l, (GLdouble)r, (GLdouble)b, (GLdouble)t);
window.set(l, r, b, t);
}
//<<setViewport>>
void Canvas::setViewport(int l, int r, int b, int t)
{
glViewport(l, b, r - l, t - b);
viewport.set(l, r, b, t);
}
IntRect Canvas::getViewport(void)
{
return viewport;
}
RealRect Canvas::getWindow(void)
{
return window;
}
void Canvas::clearScreen(void)
{
glClear(GL_COLOR_BUFFER_BIT);
}
void Canvas::lineTo(Point2 p)
{
glBegin(GL_LINES);
glVertex2f((GLfloat)CP.getX(), (GLfloat)CP.getY());
glVertex2f((GLfloat)p.getX(), (GLfloat)p.getY());
glEnd();
CP.set(p.getX(), p.getY());
glFlush();
}
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(width, height);
glutInitWindowPosition(20, 20);
glutCreateWindow(windowTitle);
setWindow(0, (float)width, 0, (float)height); // default world window
setViewport(0, width, 0, height); //default viewport
CP.set(0.0f, 0.0f); //initialize the cp to (0,0)
}
void Canvas::moveTo(Point2 p) //moves current point CP to point p object
{
float x1, y1;
x1 = p.getX();
y1 = p.getY();
CP.set(x1, y1);
}
}
}
if (n< 3)
return;
double angle=rot*3.141592/180;
double angleinc=2*3.14159265/n;
glBegin(GL_POLYGON);
for(int i=0;i<n;i++)
angle+=angleinc;
glVertex2i(radius*cos(angle)+x,radius*sin(angle)+x);
glEnd();
glFlush();
}
void polyspiral(float dist,float angle , float incr, int num)
{
for(int i=1;i<=num;i++)
{
cvs.forward (dist,2);
cvs.turn (angle);
dist= dist +incr;
}
glEnd();
glFlush();
}
}
glEnd();
glFlush();
}
void fillcolor(int n,float x,float y,float radius,float rot)
if (n< 3)
return;
double angle=rot*3.141592/180;
double angleinc=2*3.14159265/n;
glBegin(GL_POLYGON);
for(int i=0;i<n;i++)
angle+=angleinc;
glVertex2i(radius*cos(angle)+x,radius*sin(angle)+y);
}
glEnd();
glFlush();
cvs.moveTo(len*sin(ang*j)+x,len*cos(ang*j)+y);
cvs.lineTo(len*sin(ang*(j+2))+x,len*cos(ang*(j+2))+y);
}
glEnd();
glFlush();
}
void renderscene()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glClearColor(red,green,blue,0);
star(5,300.0,450.0,70);
glutSwapBuffers();
glEnd();
glFlush();
}
void processMenuEvents(int option)
{
switch(option)
{
case CIRCLE: red=1.0; green=0.0; blue=1.0;
break;
case TRIANGLE: red=0.0; green=1.0; blue=0.0;
break;
case SQUARE: red=0.0; green=0.0; blue=1.0;
break;
case STAR: red=1.0; green=1.0; blue=1.0;
break;
case PENTAGON: red=1.0; green=1.0; blue=0.0;
star(5,490.0,450.0,70);
break;
case HEXAGON: red=1.0; green=0.0; blue=1.0;
break;
case HEPTAGON: red=0.0; green=1.0; blue=1.0;
ngon(5,400.0,470.0,10,40);
break;
case OCTAGON: red=0.0; green=0.0; blue=0.0;
break;
}
glEnd();
glFlush();
}