gpt4 book ai didi

javascript - 在 HTML 5 Canvas 上创建 Reuleaux 多边形的函数

转载 作者:可可西里 更新时间:2023-11-01 14:47:32 25 4
gpt4 key购买 nike

我正在从事一个使用 HTML 5 Canvas 设计系统艺术作品的项目。为了让我的作品更加有机和多样化,我想要一个创建 reuleaux 多边形的函数。我认为可能有一种方法可以将我的 draw_sharp_polygon(center_position, radius, number_of_sides, regular, anticlockwise) 函数转换为我需要的函数,但我不确定如何执行此操作。 我需要使用大量的 context.lineTo() 函数还是我可以使用 context.arcTo() 函数某种方式?

function draw_sharp_polygon(center_position, radius, number_of_sides, regular, anticlockwise)
{
if(typeof center_position == 'undefined')
center_position = new Position();
if(typeof radius == 'undefined')
radius = dice_roll(diagonal);
if(typeof number_of_sides == 'undefined' || number_of_sides < 3)
number_of_sides = dice_roll(10);
if(typeof regular == 'undefined')
regular = coin_toss();
if(typeof anticlockwise == 'undefined')
anticlockwise = coin_toss();
context.moveTo(center_position.x + radius, center_position.y)
if(regular)
{
var circular_angle_division = (Math.PI * 2)/number_of_sides;
circular_angle_division = anticlockwise ? -1 * circular_angle_division : circular_angle_division;
for(var i = 1; i < number_of_sides; i++)
{
context.lineTo(radius * Math.cos(circular_angle_division * i),radius * Math.sin(circular_angle_division * i));
}
}
else
{
var amount_of_circle_taken = 0;
var direction = anticlockwise ? -1 : 1;
var sides_left = number_of_sides;
for(var i = 1; i < number_of_sides; i++)
{
if(i < number_of_sides -1)
{
var circular_angle_division = get_random_value(1, (((Math.PI * 2) - amount_of_circle_taken)/number_of_sides*(sides_left / 2)));
amount_of_circle_taken += circular_angle_division;
}
else
{
var circular_angle_division = (Math.PI * 2) - amount_of_circle_taken;
}
context.lineTo(radius * Math.cos(direction * circular_angle_division * i),radius * Math.sin(direction * circular_angle_division * i));
}
}
}

这就是我的平边多边形。 我想知道是否有一种方法可以使用 context.arcTo() 而不是 context.lineTo() 来创建 reuleaux 多边形。

我打算发布一个 Reuleaux 三 Angular 形的示例,但我没有足够的声誉。维基百科有一个很好的例子。

附言我还没有在我的任何个人项目中使用 jQuery,因为我觉得它们中的大多数都可以成为独立 javascript 库的一部分。 所以请不要使用 jQuery 回答。

最佳答案

arcTo() 所需的切点实际上位于圆弧端点之间的外接圆上。所需的半径可以用以下公式计算:

reuleaux_radius = radius * sqrt(2 + 2*cos(pi / number_of_sides))

相当于

reuleaux_radius = radius * sqrt(2 + 2*cos(angle2 - angle1))

完整代码:

var taget = document.getElementById('target');
var context = target.getContext('2d');

var sides = 3;
var radius = 100;
var center = new Position(150,150);

// normal polygon
context.beginPath();
draw_sharp_polygon(center, radius, sides);
context.strokeStyle = 'silver';
context.stroke();

// circle
context.beginPath();
context.arc(center.x, center.y, radius, 0, 2*Math.PI, false);
context.strokeStyle = 'silver';
context.stroke();

// reuleaux polygon
context.beginPath();
draw_reuleaux_polygon(center,radius,sides);
context.strokeStyle = 'black';
context.stroke();

function Position(x, y)
{
this.x = x || 0;
this.y = y || 0;
}

function draw_reuleaux_polygon(center_position, radius, number_of_sides)
{
if(typeof center_position == 'undefined')
throw new Error("center_position not defined");
if(typeof radius == 'undefined')
throw new Error("radius not defined");
if(typeof number_of_sides == 'undefined' || number_of_sides < 3)
throw new Error("number_of_sides not defined");

context.moveTo(center_position.x + radius, center_position.y);
for (var index1 = 0; index1 < number_of_sides; index1++)
{
// point 1 = arc start
// point 2 = tangent intersection
// point 3 = arc end
var index2 = (index1 + 0.5) % number_of_sides;
var index3 = (index1 + 1) % number_of_sides;
var angle1 = index1*2*Math.PI/number_of_sides;
var angle2 = index2*2*Math.PI/number_of_sides;
var angle3 = index3*2*Math.PI/number_of_sides;
var p1 = new Position(center_position.x + radius*Math.cos(angle1), center_position.y + radius*Math.sin(angle1));
var p2 = new Position(center_position.x + radius*Math.cos(angle2), center_position.y + radius*Math.sin(angle2));
var p3 = new Position(center_position.x + radius*Math.cos(angle3), center_position.y + radius*Math.sin(angle3));

var reuleaux_radius = radius*Math.sqrt(2 + 2*Math.cos(Math.PI/number_of_sides));

context.arcTo(p2.x, p2.y, p3.x, p3.y, reuleaux_radius);
}

context.closePath();
}

function draw_sharp_polygon(center_position, radius, number_of_sides)
{
if(typeof center_position == 'undefined')
throw new Error("center_position not defined");
if(typeof radius == 'undefined')
throw new Error("radius not defined");
if(typeof number_of_sides == 'undefined' || number_of_sides < 3)
throw new Error("number_of_sides not defined");

context.moveTo(center_position.x + radius, center_position.y);
var circular_angle_division = (Math.PI * 2)/number_of_sides;
for(var i = 1; i < number_of_sides; i++)
{
var x = center_position.x + radius * Math.cos(circular_angle_division * i);
var y = center_position.y + radius * Math.sin(circular_angle_division * i);
console.log(i,x,y);
context.lineTo(x,y);
}

context.closePath();
}
<canvas id="target" width="300" height="300"></canvas>

http://jsfiddle.net/hebvseju/

关于javascript - 在 HTML 5 Canvas 上创建 Reuleaux 多边形的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27192409/

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