ios - Create perpendicular line to CGPathRef -
i using skshapenodes dynamically draw lines updating path property user touches screen. once line complete, append new fixed length perpendicular line end of path.
i have looked cgaffinetransform rotate new line based on end point of path, far haven't had luck. tips or insight appreciated.
some of current code reference looks like:
- (void)touchesmoved:(nsset *)touches withevent:(uievent *)event { uitouch* touch = [touches anyobject]; cgpoint positioninscene = [touch locationinnode:self]; //add line new coordinates user moved cgpathaddlinetopoint(pathtodraw, null, positioninscene.x, positioninscene.y); } -(void)touchesended:(nsset *)touches withevent:(uievent *)event { //create local copy of path cgpathref mypath_ = pathtodraw; cgpoint mypoint = cgpathgetcurrentpoint(pathtodraw); //create rectangle append end of line cgrect newrect = cgrectmake(mypoint.x, mypoint.y, 25, 3); cgpathref newpath = cgpathcreatewithrect(newrect, null); //add new line end of path cgpathaddpath(newpath, null, mypath_); //set shape node path drawn path linenode.path = mypath_; }
to perpendicular, can take vector pointing in direction of last line segment, swap x , y, , invert 1 of them. this:
cgpoint v = { currentpoint.x - lastpoint.x, currentpoint.y - lastpoint.y }; cgpoint perp; if (v.x == 0) { perp.x = -v.y; perp.y = v.x; } else { perp.x = v.y; perp.y = -v.x; }
now can draw line in direction of perp
, starting @ current point, this:
cgpathmovetopoint (somepath, currentpoint); cgpathaddlinetopoint (somepath, null, currentpoint.x + perp.x * length, currentpoint.y + perp.y * length);
where length
length of line segment want draw.
and don't forget set lastpoint
currentpoint
it's correct next time around:
lastpoint = currentpoint;
Comments
Post a Comment