简体   繁体   English

如何使用SVG弧为指南针的8个方向绘制1/4弧?

[英]How to draw a 1/4 arc for the 8 directions of a compass with SVG arcs?

How do I draw an arc starting at the 8 different directions, and making a 1/4 circle?如何绘制从 8 个不同方向开始的圆弧,并制作 1/4 圆? Like a compass.就像指南针一样。 I have been trying every combination for about an hour trying to get this to be an arc like the start of an "r" with a tail, but I can't get it working.我已经尝试了大约一个小时的每种组合,试图让它成为一个像带有尾巴的“r”开头的弧线,但我无法让它工作。 I would like to see how it's done in all directions so I can get a better sense of how to manipulate it.我想看看它是如何在各个方向上完成的,这样我就可以更好地了解如何操作它。

 function polarToCartesian(centerX, centerY, radius, angleInDegrees) { var angleInRadians = (angleInDegrees-90) * Math.PI / 180.0; return { x: centerX + (radius * Math.cos(angleInRadians)), y: centerY + (radius * Math.sin(angleInRadians)) }; } console.log(describeArc(0, 250, 25, -90, 0)) function describeArc(x, y, radius, startAngle, endAngle){ var start = polarToCartesian(x, y, radius, endAngle); var end = polarToCartesian(x, y, radius, startAngle); var largeArcFlag = endAngle - startAngle <= 180 ? "0" : "1"; var d = [ "M", start.x, start.y, "A", radius, radius, 0, largeArcFlag, 0, end.x, end.y ].join(" "); return d; }
 path { float: left; stroke-linecap: round; }
 <svg style="display: inline-block; width: 50px; height: 250px" viewBox="-20 -20 120 520" xmlns="http://www.w3.org/2000/svg"> <g> <path d=" M 0 300 l 0 -50 M 0 225 A 25 25 0 0 0 -25 275" fill="none" stroke="#000" stroke-width="10px" /> </g> </svg>

I got that function off SO, but doesn't seem to work how I would expect.我把这个功能关闭了,但似乎不像我期望的那样工作。

Your function polarToCartesian is converting polar coordinates to cartesian coordinates.您的函数 polarToCartesian 正在将极坐标转换为笛卡尔坐标。
I don' think that is what you want, but you'd put the following sets of arguments in the console.log for the startAngle, endAngle我不认为这就是你想要的,但你会在 console.log 中为 startAngle、endAngle 放置以下参数集

0, 45 | 0, 45 | 45, 90 | 45, 90 | 90, 135 | 90, 135 | 135, 180 | 135, 180 | 180, 225 | 180, 225 | 225, 270 | 225, 270 | 270, 315 | 270, 315 | 315, 360 |. 315, 360 |。

The x and y args need to be calculated again as different coordinates in a cartesian coordinate space. x 和 y 参数需要再次计算为笛卡尔坐标空间中的不同坐标。 These depend on where your first coordinates start.这些取决于您的第一个坐标开始的位置。 Drawing anything that depends on going around in a circle, you need to think of where the centre of the circle is.绘制任何依赖于绕圈的东西,您需要考虑圆的中心在哪里。 The coordinates for the centre of the circle is best described as 0, 0 and then after move the circle to the desired location.圆心的坐标最好描述为 0、0,然后将圆移动到所需位置。 The coordinates are dependent on the radius, you have as 25. So I'd suggest:坐标取决于半径,你有 25。所以我建议:

0, -25 | 0, -25 | 17.677, -17.677 | 17.677, -17.677 | 25, 0 | 25, 0 | 17.677, 17.677 | 17.677, 17.677 | 0, 25 | 0, 25 | -17.677, 17.677 | -17.677, 17.677 | -25, 0 | -25, 0 | -17.677, 17.677 -17.677, 17.677

The 17.677 is obtained by the pythagorus theorem of a² + b² = c². 17.677 由 a² + b² = c² 的勾股定理得出。

And if you know the radius, because a right angled triangle can be made from the radius (as the hypotenuse) you get a triangle with equal sides with the hypotenuse at 45°.如果您知道半径,因为可以从半径(作为斜边)构成一个直角三角形,您将得到一个等边三角形,斜边为 45°。
So the length of one of the equal sides is √(c² / 2).所以等边之一的长度是√(c² / 2)。

function equalSideLength(radius){
                                 return Math.sqrt(Math.pow(radius, 2)/2);
}

To illustrate an example of output, first just showing quarters and then eighths of the circle, I'd suggest the following plan and add the stroke attribute as needed and set fill to none (whitespace added for readability and scale add to see it!).为了说明输出示例,首先只显示四分之一,然后显示圆的八分之一,我建议使用以下计划并根据需要添加笔划属性并将填充设置为无(添加空格以提高可读性和比例添加以查看它!) . And don't forget to translate the whole drawing to where you need it.并且不要忘记将整个图纸翻译到您需要的地方。

 <svg width="1000" height="1000"> <g transform="translate(100 100) scale(50)"> <path d="M0 0 L 0 -2 A2 2 0 0 1 2 0 " fill="red"/> <path d="M0 0 L 2 0 A2 2 0 0 1 0 2" fill="yellow"/> <path d="M0 0 L 0 2 A2 2 0 0 1 -2 0" fill="pink"/> <path d="M0 0 L-2 0 A2 2 0 0 1 0 -2" fill="green"/> </g> <g transform="translate(400 100) scale(50)"> <path d="M0 0 L 0 -2 A2 2 0 0 1 1.4 -1.4" fill="red"/> <path d="M0 0 L 1.4 -1.4 A2 2 0 0 1 2 0 " fill="yellow"/> <path d="M0 0 L 2 0 A2 2 0 0 1 1.4 1.4" fill="pink"/> <path d="M0 0 L 1.4 1.4 A2 2 0 0 1 0 2 " fill="green"/> <path d="M0 0 L 0 2 A2 2 0 0 1 -1.4 1.4" fill="orange"/> <path d="M0 0 L-1.4 1.4 A2 2 0 0 1 -2 0 " fill="purple"/> <path d="M0 0 L-2 0 A2 2 0 0 1 -1.4 -1.4" fill="blue"/> <path d="M0 0 L-1.4 -1.4 A2 2 0 0 1 0 -2 " fill="grey"/> </g> <svg>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM