CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2008
    Posts
    1

    Unhappy NEED a function to draw an arc using lines!

    Hi guys,
    i need to write a function in C++ which draws an arc on a graphic window using lines.. the function description follows;

    prompts the user to click on the centre Point of the circular arc and then on the starting Point of the arc circumference. Next it asks the user to click on any Point on the terminal arm of the arc. This Point does not have to be at the correct radius, since that would be almost impossible. The function draws the counterclockwise arc. If the terminal arm is located clockwise at an angle less that 180 degrees relative to the starting Point, then the reflexive angle will be drawn as shown if Figure 3..

    i tried ma best and came up with an algorithm to for this(image attached), but I'm unable to complete that. hope some one can help me out!!

    This is my code, I'm using a different class for the graphic window! All i want is just an idea, an algorithm!! even if someone can come up with something in other programming languages that would be helpful!!

    void mouseArc()
    {
    Point center = cwin.get_mouse("Click on Arc Center");
    Point start = cwin.get_mouse("Click on the arc start point on its circumference");
    Point cir = cwin.get_mouse("Click on anypoint on the CCW terminal arm of the arc");

    float cenX=center.get_x(), cenY=center.get_y(); //center point
    float srtX=start.get_x(), srtY=start.get_y(); //starting point
    float cirX=cir.get_x(), cirY=cir.get_y(); //ending point
    float tempx=0.0, tempy=0.0;

    //radoius of the arc
    float radious = sqrt(pow((srtX-cenX),2)+pow((srtY-cenY),2));

    //distance from the center to circumference point
    float distance = sqrt(pow((cirX-cenX),2)+pow((cirY-cenY),2));

    //x Axis of the original end point(unit vector*radios+centerpoint)
    float endX= ((radious*(cirX-cenX))/distance)+cenX;

    //y Axis of the Original end point(unit vector*radios+centerpoint)
    float endY= ((radious*(cirY-cenY))/distance)+cenY;
    while(srtX+(radious-radious*cos(theta))!=endX && srtY+radious*sin(theta)!=endY)
    {
    tempx=0;
    tempy=0;
    tempx= srtX+(radious-radious*cos(theta));
    tempy= srtY+radious*sin(theta);
    cwin<<Line(Point(srtX,srtY),Point(tempx,tempy));
    srtX=tempx;
    srtY=tempy;
    }

    /*for(float theta=2*M_PI/60; srtX+(radious-radious*cos(theta))!=endX && srtY+radious*sin(theta)!=endY; theta+=6)
    {
    tempx= srtX+(radious-radious*cos(theta));
    tempy= srtY+radious*sin(theta);
    cwin<<Line(Point(srtX,srtY),Point(tempx,tempy));
    srtX=tempx;
    srtY=tempy;
    }*/

    }

    i don't know whether to use the for loop or while loop!
    Attached Images Attached Images

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: NEED a function to draw an arc using lines!

    Couple of things I notice.....

    -Radius and distance are going to be the same.
    -I don't see any calls to atan2(), which is typically used for problems like this.
    -You probably want to calculate your tempx/tempy from cenX/Y, not srtX/Y.

    There may be more, I didn't look too closely.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured