Results 1 to 3 of 3

Thread: drawing geometric objects

  1. #1
    Junior Member Newbie
    Join Date
    Aug 2009
    Posts
    2

    drawing geometric objects

    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 ?

  2. #2
    Senior Member Regular Contributor
    Join Date
    Dec 2008
    Location
    USA
    Posts
    134

    Re: drawing geometric objects

    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:
    Code :
    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();
    }

  3. #3
    Super Moderator OpenGL Lord
    Join Date
    Dec 2003
    Location
    Grenoble - France
    Posts
    5,574

    Re: drawing geometric objects

    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);

Similar Threads

  1. drawing two objects with two vbo,vao
    By eniddelemaj in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 04-02-2017, 12:24 PM
  2. drawing geometric objects---2
    By lilian in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 08-24-2009, 07:48 AM
  3. Drawing objects
    By imported_Ulrich in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 06-21-2004, 01:43 PM
  4. Replies: 0
    Last Post: 12-14-2002, 07:45 PM
  5. Geometric Objects
    By Hec in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 02-22-2000, 06:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean