javascript - Extract vertices from an ARC in generic way -
i want vertices arc. have data (for ex : start point, end point, start angle, end angle, radius) used draw arc need have generate vertices arc data.
i have tried 1 or 2 algorithm failed exact vertices arc data.
i used bresenham's algorithm failed.
right using below code not working ..
double theta = 2 * 3.1415926 / 100; double c = math.cos(theta); double s = math.sin(theta); double t; double x = ((arcto) element).getradius();//we start @ angle = 0 double y = 0; for(int ii = 0; ii < 100; ii++) { coordinates.add(new coordinate(x + element.getcenterpoint().getx(), y + element.getcenterpoint().gety()));//output vertex //apply rotation matrix t = x; x = c * x - s * y; y = s * t + c * y; } please me. thank you.
first clarifications
i assume vertices mean pixels , arc standard 2d circular arc (not elliptic arc !!!) , input data are:
int (x0,y0),(x1,y1) // star/end points on curve !!! float a0,a1 // start end angles [rad] int (xc,yc) // center of circle int r // radiusdo not use bresenham
because need start 0 angle , compute pixels until start point hit. flip draw flag start filling pixel point , stop on end point hit. need handle winding match arc direction.
you can use circle parametric equation
// arc data test float r=25.0; float a0= 45.0*m_pi/180.0; float a1=270.0*m_pi/180.0; int xc=100,x0=xc+floor(r*cos(a0)),x1=xc+floor(r*cos(a1)); int yc=100,y0=yc+floor(r*sin(a0)),y1=yc+floor(r*sin(a1)); // arc rasterize code int x,y; float a,da; // here draw pixels x0,y0 , x1,y1 avoid rounding holes ... if (r) da=0.75/float(r); else da=0.1; // step less pixel avoid holes (a=a0;;a+=da) { x=xc+int(floor(r*cos(a))); y=yc+int(floor(r*sin(a))); // here draw pixel x,y if ((x==x1)&&(y==y1)) // stop if endpoint reach if (fabs(a-a1)<da) // ignore stop if not @ end angle (full or empty circle arc) break; }may
roundinstead offloorhave less pixel position error. if endpoint not match loop infinitely. if tweak bit end conditions can avoid or recomputex1,y1a1have ...you can use equation
(x-xc)^2+(y-yc)^2=r^2you need divide arc quadrants , handle each separate arc looping through
xory, computing other coordinate. loop through coordinate changing more
so in blue areas loop
y, in red loopx. example red area code can this:int x,y; (x=_x0;;x++) { y=sqrt((r*r)-((x-xc)*(x-xc))); // here draw pixel x,y if (x==_x1) // stop if endpoint reach break; }you need compute
(_x0,_y0),(_x1,_y1)start end points of cut part of arc inside quadrant , make_x0<=_x1.
the value_xlooped start/end point coordinatexc +/- sqrt(r)orx0orx1
the value_ylooped start/end point coordinateyc +/- sqrt(r)ory0ory1the blue parts done in analogically manner (just swap/replace
x,y). approach bit more complicated due cutting can done solely on integers.sqrtcan speed lut (limiting max radius) ,^2can further optimized.
[notes]
so if recapitulate parametric equation simplest implement slowest. sqrt approach can done fast bresenham (and may faster lut) need code cutting arc quadrants need few ifs prior rendering.
all codes in c++ , can further improved avoiding int/float conversions, pre-compute values before loop, etc ...
the last goes bresenham need change few things inside , when not know doing can lost. need cut octant's complexity of change far bigger sqrt approach
Comments
Post a Comment