View Full Version : drawing geometric objects

lilian

08-24-2009, 04:44 AM

Im tryin to draw a circle on clicking and dragging a mouse. Could someone suggest how could i draw the circle such that its center is the point that i clicked on and the radius is the distance till which the mouse is dragged from that point ?

todayman

08-24-2009, 09:24 AM

First, a circle is described by (r*cos(theta)+a, r*sin(theta)+b), where (a,b) is the center of the circle and r is the radius. In your case, (a,b) is the location of mouse down, and the radius is the distance between the mouse down and the current mouse position. Then, to draw the circle you can use a for loop, and increment theta from 0 to 2pi.

For instance:

void drawCircle(float mouseDownX, float mouseDownY, float newX, float newY)

{

float radius = sqrt((newX-mouseDownX)*(newX-mouseDownX) + (newY-mouseDownY)*(newY-mouseDownY);

int subdivisions = 36;

glBegin(GL_LINES);

for(int i=0; i<=subdivisions; i++) {

float theta = ((float)i)*2.0*M_PI/(float)subdivisions;

glVertex2f(radius*cos(theta)+mouseDownX, radius*sin(theta)+mouseDownY);

}

glEnd();

}

ZbuffeR

08-24-2009, 12:58 PM

And to have a GL projection so that mouse pixel coordinates map to GL coordinates, use :

const XSize = 640, YSize = 480; // replace these by your glViewport values.

glMatrixMode (GL_PROJECTION);

glLoadIdentity ();

glOrtho (0, XSize, YSize, 0, 0, 1);

glMatrixMode (GL_MODELVIEW);

Powered by vBulletin® Version 4.2.3 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.