gpt4 book ai didi

javascript - 给定正多边形的一条边,找出剩余的边

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:34:14 25 4
gpt4 key购买 nike

我们得到:

  • 正多边形的边数
  • 一侧由坐标 (x1, y1) 和 (x2, y2) 定义

我必须找出剩余的坐标。我该怎么做?

最佳答案

假设我们有一个 turtle通过从 x1, y1 移动到 x2, y2 绘制了 n 边正多边形的一部分。为了绘制剩余的边,我们执行此操作 n-1 次:

  • 转一定 Angular
  • 向前移动一定距离

我们必须转动的 Angular 是 360/n 度。例如,如果我们正在绘制一个三 Angular 形,则 turtle 必须在每一段之后转动 120 度。如果我们顺时针绘制三 Angular 形,我们将从 turtle 的当前方向减去 120 度。如果我们逆时针绘制,则增加 120 度。

向前移动的距离是第一段的长度。我们可以使用 Pythagorean theorem 计算段的长度.在 JavaScript 中,我们可以这样实现它:

var dx = x2-x1,
dy = y2-y1,
length = Math.sqrt(dx*dx + dy*dy);

turtle 的初始方向与第一条线段的 Angular 相同,我们可以通过取dx/length的反余弦来计算:

var angle = Math.acos(dx/length);
if (dy < 0) {
angle = 2*Math.PI - angle;
}

要利用转 Angular 和线段长度,我们必须实现以下 turtle 操作:

  • 设置 turtle 位置x, y
  • 设置 turtle 方向 Angular
  • 将 turtle 以当前方向向前移动 距离 个单位
  • delta 添加到 turtle 方向
  • 查询 turtle 当前位置x,y

实现这些操作后,我们可以编写一个迭代n-1 次的循环。在每次迭代中,我们转向并向前移动规定的量,查询 turtle 位置,并打印坐标。

要实现 turtle 操作,我们必须存储 turtle 的位置和方向。这是一个简单的方法:

var turtle = { x: 0, y: 0, angle: 0 };

要使 turtle 在当前方向上向前移动 distance 个单位,我们使用基本的三 Angular 函数:

turtle.x += Math.cos(turtle.angle) * distance;
turtle.y += Math.sin(turtle.angle) * distance;

请注意,JavaScript 中的三 Angular 函数使用弧度而不是度数。一个圆有 2π 弧度,因此半圆有 π 弧度。如果我们有一个以弧度表示的 Angular r,则等价于度数是 r/Math.PI * 180

当我们从 turtle 方向中添加或减去一个值时,可能会以小于零或大于 2π 的 Angular 结束。这不会影响我们的三 Angular 函数计算,但会使程序难以调试。为确保 Angular 始终在 [0, 2π) 范围内,我们可以在 turtle.angle 被修改时执行以下操作:

turtle.angle -= Math.floor(turtle.angle / (2*Math.PI)) * 2*Math.PI;

我已经编写了一个代码片段来演示 turtle 方法。单击下面的蓝色按钮运行代码,然后单击并拖动以绘制多边形的第一段。您可以通过单击加号和减号来更改边数。

var Polygon = {
color: {
axes: '#ccc',
sides: {
hover: { plain: '#dddfa4', special: '#9d9c64' },
final: { plain: '#b0c598', special: '#4f7337' }
}
}
};

Polygon.turtle = { x: 0, y: 0, angle: 0 };
Polygon.turtle.setPosition = function (x, y) {
var g = Polygon,
turtle = g.turtle,
context = g.context,
origin = g.origin;
turtle.x = x;
turtle.y = y;
context.moveTo(origin.left + turtle.x, origin.top - turtle.y);
};
Polygon.turtle.setAngle = function (angle) {
var g = Polygon,
turtle = g.turtle;
turtle.angle = angle;
};
Polygon.turtle.left = function (delta) {
var g = Polygon,
turtle = g.turtle;
turtle.angle = g.normalizeAngle(turtle.angle + delta);
};
Polygon.turtle.right = function (delta) {
var g = Polygon,
turtle = g.turtle;
turtle.angle = g.normalizeAngle(turtle.angle - delta);
};
Polygon.normalizeAngle = function (angle) {
angle -= Math.floor(angle / (2*Math.PI)) * 2*Math.PI;
return angle;
};
Polygon.turtle.forward = function (distance) {
var g = Polygon,
turtle = g.turtle,
canvas = g.canvas,
context = g.context,
origin = g.origin;
turtle.x += Math.cos(turtle.angle) * distance;
turtle.y += Math.sin(turtle.angle) * distance;
context.lineTo(origin.left + turtle.x, origin.top - turtle.y);
};

Polygon.resizeCanvas = function() {
var g = Polygon,
canvas = g.canvas,
context = g.context,
width = canvas.width = window.innerWidth,
height = canvas.height = window.innerHeight;
g.origin = { left: Math.floor(width/2), top: Math.floor(height/2) };
g.drawAxes();
};
Polygon.drawAxes = function() {
var g = Polygon,
canvas = g.canvas,
context = g.context,
origin = g.origin,
color = g.color;
context.lineWidth = 2;
context.strokeStyle = color.axes;
context.beginPath();
context.moveTo(origin.left, 0);
context.lineTo(origin.left, canvas.height);
context.moveTo(0, origin.top);
context.lineTo(canvas.width, origin.top);
context.stroke();
};
Polygon.drawPolygon = function (situation) {
var g = Polygon,
canvas = g.canvas,
context = g.context,
turtle = g.turtle,
color = g.color,
n = parseInt(document.getElementById('numSides').innerHTML, 10),
turn = 2*Math.PI / n,
x1 = g.x1, y1 = g.y1, x2 = g.x2, y2 = g.y2,
dx = x2-x1,
dy = y2-y1,
length = Math.sqrt(dx*dx + dy*dy);
var angle = Math.acos(dx/length);
if (dy < 0) {
angle = 2*Math.PI - angle;
}
context.clearRect(0, 0, canvas.width, canvas.height);
g.drawAxes();
context.lineWidth = 4;
context.lineCap = 'round';
context.beginPath();
context.strokeStyle = color.sides[situation].plain;
turtle.setPosition(x1, y1);
turtle.setAngle(angle);
for (var i = 0; i < n; ++i) {
turtle.forward(length);
turtle.left(turn);
}
context.closePath();
context.stroke();
context.strokeStyle = color.sides[situation].special;
context.beginPath();
turtle.setPosition(x1, y1);
turtle.forward(length);
context.stroke();
}
Polygon.load = function () {
var g = Polygon,
canvas = g.canvas = document.getElementById('surface'),
context = g.context = canvas.getContext('2d'),
display = { begin: document.getElementById('begin'),
end: document.getElementById('end') },
color = g.color;
g.resizeCanvas();
window.onresize = g.resizeCanvas;
function makeUnselectable(element) {
element.className += ' unselectable';
element.ondragstart = element.onselectstart = function (event) {
event.preventDefault();
};
}
makeUnselectable(canvas);
var numSides = document.getElementById('numSides'),
minus = document.getElementById('minus'),
plus = document.getElementById('plus');
minus.onmousedown = function () {
var current = parseInt(numSides.innerHTML, 10);
if (current == 3) {
return;
}
numSides.innerHTML = current-1;
g.drawPolygon('final');
};
plus.onmousedown = function () {
var current = parseInt(numSides.innerHTML, 10);
if (current == 20) {
return;
}
numSides.innerHTML = current+1;
g.drawPolygon('final');
};
var controls = [display.begin, display.end, numSides, minus, plus,
document.getElementById('options')];
for (var i = 0; i < controls.length; ++i) {
makeUnselectable(controls[i]);
}
var getPosition = function (event) {
event = event || window.event;
var rect = canvas.getBoundingClientRect(),
left = event.clientX - rect.left,
top = event.clientY - rect.top,
origin = g.origin,
x = left - origin.left,
y = origin.top - top;
return { x: x, y: y };
};
canvas.onmousedown = function (event) {
document.body.style.cursor = 'default';
var position = getPosition(event);
g.x1 = g.x2 = position.x;
g.y1 = g.y2 = position.y;
display.begin.innerHTML =
'<span class="label">x1, y1 =</span> '+g.x1+', '+g.y1;
display.end.innerHTML = '';
g.drawPolygon('hover');
for (var i = 0; i < controls.length; ++i) {
controls[i].style.zIndex = -10;
}
canvas.onmousemove = function (event) {
var position = getPosition(event);
g.x2 = position.x;
g.y2 = position.y;
display.end.innerHTML =
'<span class="label">x2, y2 =</span> '+g.x2+', '+g.y2;
g.drawPolygon('hover');
};
};
function noop() {
}
canvas.onmousemove = noop;
canvas.onmouseup = canvas.onmouseout = function (event) {
if (canvas.onmousemove === noop) {
return;
}
canvas.onmousemove = noop;
g.drawPolygon('final');
for (var i = 0; i < controls.length; ++i) {
controls[i].style.zIndex = 0;
}
};
};
window.onload = Polygon.load;
body {
margin: 0;
padding: 0;
overflow: hidden;
}
.unselectable {
-webkit-user-select: none;
-khtml-user-drag: none;
-khtml-user-select: none;
-moz-user-select: none;
-moz-user-select: -moz-none;
-ms-user-select: none;
user-select: none;
}
canvas {
width: 100%;
height: 100%;
}
.display {
color: #444;
position: fixed;
left: 40px;
font-family: sans-serif;
font-size: 20px;
}
.label {
color: #aaa;
}
#begin {
top: 20px;
}
#end {
top: 60px;
}
#options {
position: fixed;
left: 40px;
top: 100px;
font-family: sans-serif;
font-size: 28px;
}
#options div {
display: inline;
}
#options .button {
font-size: 32px;
cursor: pointer;
}
#options .button:hover {
color: #55838e;
}
#options .button, #numSides {
padding: 0 5px;
}
#numSides {
cursor: default;
}
<div class="display" id="begin"></div>
<div class="display" id="end"></div>

<div id="options">
<div class="button" id="minus">&minus;</div><div
id="numSides">6</div><div
class="button" id="plus">&plus;</div>
</div>

<canvas id="surface"></canvas>

关于javascript - 给定正多边形的一条边,找出剩余的边,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29860883/

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