gpt4 book ai didi

javascript - 使用 Canvas Logic 在三 Angular 形中获得 180 度

转载 作者:行者123 更新时间:2023-11-30 08:27:53 24 4
gpt4 key购买 nike

我正在尝试创建一个小程序,其中三 Angular 形中的所有度数加起来为 180 度。到目前为止,我所拥有的是,当我使用 Math.Random 方法时,我得到了一个随机数,但有些度数加起来不等于 180°。我已经尝试使用 if 条件语句,但到目前为止没有成功。

这是我的代码:

https://jsfiddle.net/14p880p9/

    var a = Math.round((Math.random() * 100) + 1);
var b = Math.round((Math.random() * 100) + 1);
var c = Math.round((Math.random() * 100) + 1);

var d = a + b + c;



ctx.beginPath();
ctx.moveTo(50, 10);
ctx.lineTo(50, 450);
ctx.lineTo(650, 400);
ctx.closePath();
ctx.lineWidth = 10;
ctx.strokeStyle = '#666666';
ctx.stroke();
ctx.fillStyle = "#FFCC00";
ctx.fill();

ctx.font = "30px Comic Sans MS";
ctx.fillStyle = "black";
ctx.fillText(a + "°",52,60);


//width distance, and then altitude
ctx.font = "30px Comic Sans MS";
ctx.fillStyle = "black";
ctx.fillText(b+ "°",60,420);



//width distance, and then altitude
ctx.font = "30px Comic Sans MS";
ctx.fillStyle = "black";
ctx.fillText(c + "°",570,400);

最佳答案

使用这个,它可以生成任何组合,不像上面的限制选项,其中一个边必须是 1-100。

var a = Math.round((Math.random() * 90) + 1);
var b = Math.round((Math.random() * 90) + 1);
var c = 180 - a - b;

关于javascript - 使用 Canvas Logic 在三 Angular 形中获得 180 度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42405581/

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