简体   繁体   English

如何在三边JS上绘制三角形?

[英]How to draw a triangle on three sides JS?

I will have 3 inputs and a button. 我将有3个输入和一个按钮。 When you click on this button you should draw a triangle in which the lengths of 3 sides are equal to the values in the inputs. 单击此按钮时,应绘制一个三角形,其中3边的长度等于输入中的值。 How to organize it? 如何组织? I found this: 我找到了这个:

context.beginPath();
context.moveTo(30, 20);
context.lineTo(some_value1,some_value2);
context.lineTo(some_value3,some_value4);
context.closePath();

But this method is suitable for drawing, on two sides. 但是此方法适合在两侧绘制。 I hope now I have described the problem in detail. 我希望现在我已经详细描述了这个问题。 Thanks in advance. 提前致谢。

You will need to compute cartesian coordinates of each point first. 您将需要首先计算每个点的笛卡尔坐标。 You can set the first point to [0, 0] , and the second will be at [x1, 0] where x1 is length of first line. 您可以将第一个点设置为[0, 0] ,第二个点将位于[x1, 0] ,其中x1是第一行的长度。 Third point needs to be calculated, take a look here how this can be done: 需要计算第三点,在这里看看如何做到这一点:

https://math.stackexchange.com/questions/543961/determine-third-point-of-triangle-when-two-points-and-all-sides-are-known https://math.stackexchange.com/questions/543961/determine-third-point-of-triangle-when-two-points-and-all-sides-众所周知

 // hardcoded, but you would get those from user var AB = 40; var BC = 50; var AC = 30; var A = [0, 0]; // starting coordinates var B = [0, AB]; var C = []; // calculate third point C[1] = (AB * AB + AC * AC - BC * BC) / (2 * AB); C[0] = Math.sqrt(AC * AC - C[1] * C[1]); console.log(A, B, C); var canvas = document.getElementById('canvas'); var ctx = canvas.getContext('2d'); ctx.beginPath(); ctx.moveTo(A[0], A[1]); ctx.lineTo(B[0], B[1]); ctx.lineTo(C[0], C[1]); ctx.fill(); 
 <canvas id="canvas" width="500" height="500"></canvas> 

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

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