[英]Draw the angle marks of a triangle in canvas
I've been striving for while trying to draw the angle marks of a triangle in canvas and got nothing so far. 我一直在努力尝试在画布上绘制三角形的角度标记,但到目前为止没有任何帮助。 I've tried using the arc method without success. 我尝试使用arc方法没有成功。 Maybe I lack the math knowledge necessary to do this so I just ask please give me a function that takes 3 points ({x,y} type of objects) and draws a figure simillar to this one: 也许我缺乏执行此操作所需的数学知识,所以我只想问一下我给我一个函数,该函数需要3点({x,y}类型的对象)并为此绘制一个相似的图形:
thanks! 谢谢!
Here is how to do it.. 这是方法。
Two handy functions distance
and direction
get the length of a line and the direction of a line. 两个方便的功能, distance
和direction
获取线的长度和线的方向。 Because direction is cyclic we "in this case" keep it positive and the modulo of 2PI (360 degrees) and we have to do a little checking because the angles we get between lines may be negative even though one may be 300Deg and the other 370Deg but will appear as 300Deg and 10Deg. 因为方向是循环的,所以我们“在这种情况下”将其保持为正,并保持2PI(360度)的模,并且我们必须做一点检查,因为即使两行之间的夹角可能为300Deg,而另一行为370Deg,我们在两行之间得到的角度也可能为负。但将显示为300Deg和10Deg。
There is lots of room for optimization as a lot of stuff is recalculated, and the arc can at times cross a line if the triangle has a very long edge with an inner angle close to PI. 由于需要重新计算很多内容,因此还有很多优化的余地,如果三角形的边缘非常长且内角接近PI,则弧有时会越过一条直线。
The angle stroke radius is 1/5th of the shortest line. 角度笔划半径为最短线的1/5。 You may want to set this per angle. 您可能需要针对每个角度进行设置。 I have not included the text render but just get half the sweepAng
add to startAngle
in function drawAngle
to get x and y for the text. 我没有包括文本渲染,只是在函数drawAngle
一半的sweepAng
添加到startAngle
以获得文本的x和y。
var textX = x + Math.cos(startAngle + sweepAng / 2) * minDist *2;
var textY = y + Math.sin(startAngle + sweepAng / 2) * minDist *2;
Think I have plenty of comments to help but do ask if you do not understand. 认为我有很多意见可以帮助您,但请问您是否不明白。
// ctx is the canvas context
// function to get distance
function distance(x, y, xx, yy) {
return Math.sqrt(Math.pow(x - xx, 2) + Math.pow(y - yy, 2) );
}
// function gets the direction of a line
function direction(x, y, xx, yy) {
var angV = Math.acos( (xx - x) / Math.sqrt( Math.pow(x - xx, 2) + Math.pow(y - yy, 2) ) );
if (y - yy > 0) angV = - angV; // check the sign
return (angV + Math.PI * 2) % (Math.PI * 2); // makes the angle positive.
// Not needed but for this
// makes it easier to understand
}
// function to draw a triangle with angle marks
// pass it the 3 points at the corners of the triangle.
// will handle any triangle
function drawTriangle(x1,y1,x2,y2,x3,y3){
// function to draw angle
function drawAngle(x, y, dirA, dirB){
dirB += Math.PI; // revers second direction
var sweepAng = dirB - dirA; // angle between lines
var startAng = dirA; // angle to start the sweep of the arc
if(Math.abs(sweepAng) > Math.PI){ // if the angle us greater the 180
sweepAng = Math.PI * 2 - sweepAng; // get the smaller angle
startAng = dirB; // and change the start angle
}
ctx.beginPath();
if(sweepAng < 0){ // if the angle is sweeping anticlockwise
ctx.arc(x, y, minDist ,startAng + sweepAng , startAng);
}else{ // draw clockwise angle
ctx.arc(x, y, minDist, startAng, startAng + sweepAng);
}
ctx.stroke(); // all done
}
ctx.lineWidth = 3; // draw the lines of the triangle
ctx.strokeStyle = "black";
ctx.beginPath();
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.lineTo(x3, y3);
ctx.closePath();
ctx.stroke();
// now work out the radius of the angle stroke
var dist1 = distance(x1, y1, x2, y2); // get the 3 distance of the lines
var dist2 = distance(x2, y2, x3, y3);
var dist3 = distance(x3, y3, x1, y1);
var minDist = Math.min(dist1, dist2, dist3); // get the min dist;
if(minDist === 0){
return; // there are no angles to draw and exit
// to avoid divide by zero in direction function
}
minDist /= 5; // get the amgle arc radius 1/5th
var dir1 = direction(x1, y1, x2, y2); // get the 3 directions of the lines
var dir2 = direction(x2, y2, x3, y3);
var dir3 = direction(x3, y3, x1, y1);
drawAngle(x1, y1, dir1, dir3); // draw the angle stoke first corner;
drawAngle(x2, y2, dir2, dir1); // draw the angle stoke second corner;
drawAngle(x3, y3, dir3, dir2); // draw the angle stoke third;
}
Have not run it so hope there are no typos. 没有运行它,所以希望没有错别字。 Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.