In: Computer Science
Using OpenGL develop a C code that a) draws a point that moves clockwise slowly along a circle with a center at (0,0). You may want to draw a circle first, and a point that moves on the circle with a different color. b) draws a 2D star inside the circle.
PROGRAM: void DrawArc(float cx, float cy, float r, float start_angle, float arc_angle, int num_segments) { float theta = arc_angle / float(num_segments - 1);//theta is now calculated from the arc angle instead, the - 1 bit comes from the fact that the arc is open float tangetial_factor = tanf(theta); float radial_factor = cosf(theta); float x = r * cosf(start_angle);//we now start at the start angle float y = r * sinf(start_angle); glBegin(GL_LINE_STRIP);//since the arc is not a closed curve, this is a strip now for(int ii = 0; ii < num_segments; ii++) { glVertex2f(x + cx, y + cy); float tx = -y; float ty = x; x += tx * tangetial_factor; y += ty * tangetial_factor; x *= radial_factor; y *= radial_factor; } glEnd(); }
//========================================================================
(B)
#include <GL/glut.h>
#include <GL/gl.h> #include <GL/freeglut.h> void init (void) { glClearColor(0.0,0.0,0.0,00); glMatrixMode(GL_PROJECTION); gluOrtho2D(0.0,200.0,0.0,200.0); } void LineSegment(void) { glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0,1.0,1.0); glBegin(GL_LINE_LOOP); glVertex2i(20,120); glVertex2i(180,120); glVertex2i(45,20); glVertex2i(100,190); glVertex2i(155,20); glEnd(); glFlush(); } int main(int argc,char** argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowPosition(50,100); glutCreateWindow("STAR"); init(); glutDisplayFunc(LineSegment); glutMainLoop(); return 0; }
//=======================================================================