core graphics - iOS: Rotating a line with respect to a point that could be adjusted by touch -


line rotation around specific point

man, have been struggling this...

in reference image above, have line fixed specific point. @ other end of line have point not fixed , dragged position on screen. need line able follow point , rotated respect it's position without changing lengths.

i know of accomplished quite without having line fixed length.

you draw uibezierpath in drawrect: this:

uibezierpath *bezpath = [uibezierpath bezierpath]; [bezpath movetopoint:fixedpointstart]; [bezpath addlinetopoint:draggedpointend]; [bezpath stroke]; 

then call [self setneedsdisplay] in -(void)touchesmoved:(nsset *)touches withevent:(uievent *)event in uiview pass new coordinates of point dragged , have redrawn.

but mentioned, need line fixed length.

one other thing (not sure if make difference), line part of larger uibezierpath of irregular shape. "specific fixed point" shown in picture point of attachement of line larger irregular uibezierpath. line drawn part of same uibezierpath see in following code.

so here have now:

//    creating fixed point here being returned custom method  finds point of attachement larger irregular shaped uibezierpath. //    draggable points uiview's , i'm using origins.  cgpoint fixedpoint = [apath findcurvebezierpathpointinbetweenthestartpoint:point1.frame.origin                                                              theendpoint:topcurvepoint.frame.origin                                         withfirstcontrolpoint:topcurvefirstcontrolpoint.frame.origin                    andsecondcontrolpoint:topcurvesecondcontrolpoint.frame.origin atpercentage:0.3];     //    fixed point cgpoint startpoint = fixedpoint;  //    point going able dragged cgpoint endpoint = topcurvefirstcontrolpoint.frame.origin;   //    finding angle in between these 2 points pass in cgaffinetransform rotation.  float angle = [self getrotatingangle:startpoint secondpoint:endpoint]; nslog(@"angle in radians: %f",angle);    //    creating transform passing in angle , doing necessary translations rotation occur on fixed point.  cgaffinetransform transform = cgaffinetransformmaketranslation(fixedpoint.x, fixedpoint.y); transform = cgaffinetransformrotate(transform, angle); transform = cgaffinetransformtranslate(transform,-fixedpoint.x,-fixedpoint.y);   //  setting draw line of fixed length , applying transform ,  [apath movetopoint:startpoint]; [apath addlinetopoint:cgpointmake(fixedpoint.x, fixedpoint.y - 60)]; [apath applytransform:transform];    // continue draw rest of bezierpath , stroke @ end.     [apath movetopoint:point1.frame.origin];  [apath addcurvetopoint:topcurvepoint.frame.origin controlpoint1:topcurvefirstcontrolpoint.frame.origin controlpoint2:topcurvesecondcontrolpoint.frame.origin]; 

so question is: how find correct angle have fixed line transformed every time drawrect:is called when draggable point finds new position?

the method use find angle between fixed point , dragged point gives me correct angle have tested it. however, can't figure out how apply angle between 2 points in way rotate line correctly in respect draggable point.

edit: added link better explanation of i'm trying accomplish exactly.

why transforming it? simple geometry. can find angle between centre , current point. since length equal path circle. find point on circle using centre point , angle. suppose after dragging touch point currentpoint. find angle use:

cgfloat angle = atan2f(currentpoint.y - centre.y, currentpoint.x - centre.x); 

now find point on circle corresponds above angle:

finalpoint.x = centre.x + length * cosf(angle); finalpoint.y = centre.y + length * sinf(angle); 

where length fixed length. can draw line between centre , finalpoint using quartz-2d.

note: angle can become negative. may in implementation want convert equivalent +ve angle.

if (angle < 0)      angle += 2 * m_pi; 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -