Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

( Help with part B ) Modify the code developed on the homework # 1 to a ) Translate ( move ) the whole object

(Help with part B) Modify the code developed on the homework #1 to
a) Translate (move) the whole object ( the star, the circle and the point that moves around) along the x axis using the -> and <- keys on the keyboard
b) Rotate the whole object around the z axis using the shift -> and shift <- keys on the keyboard.
(Here's the already developed code, part A is already done.)
#include
#include
const int windowWidth =650;
const int windowHeight =600;
const float circleRadius =0.5f;
const float pointSize =5.0f;
const float pointSpeed =0.01f;
float pointAngle =0.0f;
float figurePositionX =0.0f; // Initial x-position of the figures
void init(){
glClearColor(0.15f,0.10f,0.20f,1.0f); //color violeta dark
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-1.0,1.0,-1.0,1.0);
}
void drawCircle(){
glColor3f(1.0,0.0,1.0); //color rosa
glBegin(GL_LINE_LOOP);
for (int i =0; i <360; ++i){
float angle = i *3.14159/180;
glVertex2f(circleRadius * cos(angle)+ figurePositionX, circleRadius * sin(angle));
}
glEnd();
}
void drawPoint(){
glColor3f(0.0,1.0,0.0); //color verde
glPointSize(pointSize);
glBegin(GL_POINTS);
glVertex2f(circleRadius * cos(pointAngle)+ figurePositionX, circleRadius * sin(pointAngle));
glEnd();
}
void drawStar(){
glColor3f(1.0,1.0,0.0); //color amarillo
glLineWidth(3.0); //intentando hacer la estrella mas gruesa
float innerRadius = circleRadius *0.382f;
glPushMatrix();
glTranslatef(figurePositionX,0.0f,0.0f); // Translate along x-axis
glRotatef(90.0f,0.0f,0.0f,1.0f); //enderezala!
glBegin(GL_LINE_LOOP);
for (int i =0; i <360; i +=72){
float angle = i *3.14159/180;
glVertex2f(circleRadius * cos(angle), circleRadius * sin(angle));
angle +=36*3.14159/180;
glVertex2f(innerRadius * cos(angle), innerRadius * sin(angle));
}
glEnd();
glPopMatrix();
}
void display(){
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
drawCircle();
drawStar();
drawPoint();
glutSwapBuffers();
}
void update(int value){
pointAngle -= pointSpeed;
if (pointAngle <0.0)
pointAngle
const int windowWidth =650;
const int windowHeight =600;
const float circleRadius =0.5f;
const float pointSize =5.0f;
const float pointSpeed =0.01f;
float pointAngle =0.0f;
float figurePositionX =0.0f; // Initial x-position of the figures
void init(){
glClearColor(0.15f,0.10f,0.20f,1.0f); //color violeta dark
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-1.0,1.0,-1.0,1.0);
}
void drawCircle(){
glColor3f(1.0,0.0,1.0); //color rosa
glBegin(GL_LINE_LOOP);
for (int i =0; i <360; ++i){
float angle = i *3.14159/180;
glVertex2f(circleRadius * cos(angle)+ figurePositionX, circleRadius * sin(angle));
}
glEnd();
}
void drawPoint(){
glColor3f(0.0,1.0,0.0); //color verde
glPointSize(pointSize);
glBegin(GL_POINTS);
glVertex2f(circleRadius * cos(pointAngle)+ figurePositionX, circleRadius * sin(pointAngle));
glEnd();
}
void drawStar(){
glColor3f(1.0,1.0,0.0); //color amarillo
glLineWidth(3.0); //intentando hacer la estrella mas gruesa
float innerRadius = circleRadius *0.382f;
glPushMatrix();
glTranslatef(figurePositionX,0.0f,0.0f); // Translate along x-axis
glRotatef(90.0f,0.0f,0.0f,1.0f); //enderezala!
glBegin(GL_LINE_LOOP);
for (int i =0; i <360; i +=72){
float angle = i *3.14159/180;
glVertex2f(circleRadius * cos(angle), circleRadius * sin(angle));
angle +=36*3.14159/180;
glVertex2f(innerRadius * cos(angle), innerRadius * sin(angle));
}
glEnd();
glPopMatrix();
}
void display(){
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
drawCircle();
drawStar();
drawPoint();
glutSwapBuffers();
}
void update(int value){
pointAngle -= pointSpeed;
if (pointAngle <0.0)
pointAngle +=2*3.14159;
glutPostRedisplay();
glutTimerFunc(16, update, 0);
}
void specialKeys(int key, int x, int y){
switch (key){
case GLUT_KEY_LEFT:
figurePositionX -=0.1f; // Move figures left
break;
case GLUT_KEY_RIGHT:
figurePositionX +=0.1f; // Move figures right
break;
}
glutPostRedisplay();
}
int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(windowWidth, windowHeight);
glutCreateWindow("Star and Moving Point");
init();
glutDisplayFunc(display);
glutSpecialFunc(specialKeys); // Register special key callback
glutTimerFunc(0, update, 0);
glutMainLoop();
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Practical Database Programming With Visual C# .NET

Authors: Ying Bai

1st Edition

0470467274, 978-0470467275

More Books

Students also viewed these Databases questions

Question

using signal flow graph

Answered: 1 week ago