如何使用两点(起点向量和终点向量)在场景视图中的各个方向绘制箭头,请参见附件绘制箭头,如上侧、下侧、左侧上下、右侧上下。我尝试使用 UIBezierPath 绘制箭头,但箭头形状看起来与箭头图像不完全相同 它也不适用于所有方向
检查下面我应用的代码,我如何修复与图像相同的形状的任何解决方案,或者是否有任何使用两点如何在场景视图中绘制方向箭头的解决方案
let path = UIBezierPath.arrow(from: CGPoint(x: arrowstartx, y: arrowstarty), to: CGPoint(x: arrowendx, y: arrowendy), tailWidth: 0.02, headWidth: 0.02, headLength: 0.02)
path.close()
let arrowShape = SCNShape(path: path, extrusionDepth: 0.001)
let arrowNode = SCNNode(geometry: arrowShape)
arrowNode.geometry?.firstMaterial?.diffuse.contents = UIColor(hexString: selectedItem ?? myMarkupDefaultColor)
arrowNode.position = SCNVector3((vector1.x+vector3.x)/2.0, (vector1.y+vector3.y)/2.0, (vector1.z+vector3.z)/2.0)
arrowNode.scale = SCNVector3(1.0,1.0, 1.0)
self.sceneViewARMarkup.scene.rootNode.addChildNode(arrowNode)
class func arrow(from start: CGPoint, to end: CGPoint, tailWidth: CGFloat, headWidth: CGFloat, headLength: CGFloat) -> Self {
let length = hypot(end.x - start.x, end.y - start.y)
let tailLength = length - headLength
func p(_ x: CGFloat, _ y: CGFloat) -> CGPoint { return CGPoint(x: x, y: y) }
let points: [CGPoint] = [
p(0, tailWidth / 2),
p(tailLength, tailWidth / 2),
p(tailLength, headWidth / 2),
p(length, 0),
p(tailLength, -headWidth / 2),
p(tailLength, -tailWidth / 2),
p(0, -tailWidth / 2)
]
let cosine = (end.x - start.x) / length
let sine = (end.y - start.y) / length
let transform = CGAffineTransform(a: cosine, b: sine, c: -sine, d: cosine, tx: start.x, ty: start.y)
let path = CGMutablePath()
path.addLines(between: points, transform: transform )
path.closeSubpath()
return self.init(cgPath: path)