c# - Always return a CCW rotation from a 3 point arc method -
i'm trying create 3 point arcs table autocad (r12/lt2) dxf file. problem comes file seems save arcs in ccw rotation. method below works if pont data feed ccw not work if rotation cw arc's angles gets 'flipped'.i can't seem figure out way detect when cw arc's data detected rectify error
the method calculates arc 3 points on arc's circumference (not start, center , end point)
here method:
public static list<double> threepointarch(double startx, double starty, double midx, double midy, double endx, double endy) { list<double> returnvalues = new list<double>(); //calculate center point 3 points on circle //calculate line r double liner = (midy - endy) / (midx - endx); //calculate line t double linet = (starty - midy) / (startx - midx); //calculate x center double centerx = ((liner * linet * (starty - endy)) + (liner * (midx + startx)) - (linet * (endx + midx))) / (2 * (liner - linet)); //calculate y center subsitution double centery = ((endy + midy) / 2) + ((centerx - ((endx + midx) / 2)) * (-1 / liner)); //calculate raduis double raduis = math.sqrt((startx - centerx) * (startx - centerx) + (starty - centery) * (starty - centery)); //calculate start angle double startangle = math.atan2(starty - centery, startx - centerx) * (180 / math.pi); //calculate end angle double endtangle = math.atan2(endy - centery, endx - centerx) * (180 / math.pi); //if (endtangle < 0) //{ // endtangle = endtangle + 360; //} returnvalues.add(centerx); returnvalues.add(centery); returnvalues.add(raduis); returnvalues.add(startangle); returnvalues.add(endtangle); return returnvalues; }
i have been trying 3 weeks every fix think of, tried calculating midpoint angle see if less endpoint angle, tried see if startpoint angle less endpoint angle(with several variations of this)this math on head!!
edit:
i found information here , came this(added above return):
//calculate rotation double midangle = math.atan2(midy - centery, midx - centerx) * (180 / math.pi); if (midangle - startangle > +180.0) midangle -= 360.0; if (midangle - startangle < -180.0) midangle += 360.0; if (endtangle - midangle > +180.0) endtangle -= 360.0; if (endtangle - midangle < -180.0) endtangle += 360.0; string dir = "none"; if (midangle - startangle < 0) dir = "cw"; if (midangle - startangle > 0) dir = "ccw"; if (midangle - startangle == 0) dir = "none"; if (dir == "cw") { double startangletmp = startangle; startangle = endtangle; endtangle = startangletmp; }
it seems work part if can confirmation math head, great
i assume in 2d use cross product detect cw/ccw
let p0,p1,p2
arcs points 3d vectors (x,y,z=0.0)
then:
v0=p1-p0 v1=p2-p1
are edge vectors. compute cross product
n=cross(v0,v1)
returning normal vector n
of plane arc lies on test sign of z
coordinate:
if (n.z<0.0) cw else ccw
the sign depends on configuration of coordinate system if not work use
if (n.z>0.0) cw else ccw
if (n.z==0.0)
in trouble , try avoid @ costs dividing such entities 2 arc's.
Comments
Post a Comment