gpt4 book ai didi

iphone - 使用 CGContext 在直线上绘制三角形/箭头

转载 作者:行者123 更新时间:2023-12-03 18:26:38 26 4
gpt4 key购买 nike

我正在使用route-me 框架来处理位置。在此代码中,两个标记(点)之间的路径将绘制为一条线。

我的问题:“如果我想在线的中间(或顶部)添加一个箭头,以便它指向方向,我应该添加什么代码”

谢谢



- (void)drawInContext:(CGContextRef)theContext
{
renderedScale = [contents metersPerPixel];

float scale = 1.0f / [contents metersPerPixel];

float scaledLineWidth = lineWidth;
if(!scaleLineWidth) {
scaledLineWidth *= renderedScale;
}
//NSLog(@"line width = %f, content scale = %f", scaledLineWidth, renderedScale);

CGContextScaleCTM(theContext, scale, scale);

CGContextBeginPath(theContext);
CGContextAddPath(theContext, path);

CGContextSetLineWidth(theContext, scaledLineWidth);
CGContextSetStrokeColorWithColor(theContext, [lineColor CGColor]);
CGContextSetFillColorWithColor(theContext, [fillColor CGColor]);

// according to Apple's documentation, DrawPath closes the path if it's a filled style, so a call to ClosePath isn't necessary
CGContextDrawPath(theContext, drawingMode);
}

最佳答案

- (void) drawLine: (CGContextRef) context from: (CGPoint) from to: (CGPoint) to 
{
double slopy, cosy, siny;
// Arrow size
double length = 10.0;
double width = 5.0;

slopy = atan2((from.y - to.y), (from.x - to.x));
cosy = cos(slopy);
siny = sin(slopy);

//draw a line between the 2 endpoint
CGContextMoveToPoint(context, from.x - length * cosy, from.y - length * siny );
CGContextAddLineToPoint(context, to.x + length * cosy, to.y + length * siny);
//paints a line along the current path
CGContextStrokePath(context);

//here is the tough part - actually drawing the arrows
//a total of 6 lines drawn to make the arrow shape
CGContextMoveToPoint(context, from.x, from.y);
CGContextAddLineToPoint(context,
from.x + ( - length * cosy - ( width / 2.0 * siny )),
from.y + ( - length * siny + ( width / 2.0 * cosy )));
CGContextAddLineToPoint(context,
from.x + (- length * cosy + ( width / 2.0 * siny )),
from.y - (width / 2.0 * cosy + length * siny ) );
CGContextClosePath(context);
CGContextStrokePath(context);

/*/-------------similarly the the other end-------------/*/
CGContextMoveToPoint(context, to.x, to.y);
CGContextAddLineToPoint(context,
to.x + (length * cosy - ( width / 2.0 * siny )),
to.y + (length * siny + ( width / 2.0 * cosy )) );
CGContextAddLineToPoint(context,
to.x + (length * cosy + width / 2.0 * siny),
to.y - (width / 2.0 * cosy - length * siny) );
CGContextClosePath(context);
CGContextStrokePath(context);
}

关于iphone - 使用 CGContext 在直线上绘制三角形/箭头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2500197/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com