本文介绍了绘制内接于圆的正多边形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试绘制内接在给定中心 (x,y) 和半径 (r) 的圆中的正多边形(正方形和等边三角形).我正在使用 raphael.js.
I'm trying to draw regular polygons(square and equilateral triangle) inscribed in a circle of a given centre (x,y) and a radius (r). I'm using raphael.js.
这是我绘制内接正方形的函数:
Here's my function to draw a inscribed square:
function draw_square(x,y,radius){
var side= radius*(Math.sqrt(2));
var x = x - (side/2);
var y = y - (side/2);
var square= paper.rect(x, y, side, side);
}
谁能解释一下我如何绘制等边三角形(内接给定的圆)?
Can anyone shed some light on how I could draw an equilateral triangle(inscribed in a given circle)?
推荐答案
我第一次使用 raphael,所以您必须从以下内容中提取您需要的内容:
First time I've used raphael, so you'll have to extract what you need from the following:
<html>
<body>
<div id="paper"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/raphael/2.1.4/raphael-min.js"></script>
<script>
var paper = new Raphael(document.getElementById('paper'), 256, 256);
var x = 128, y = 128, r = 64, n = 9;
paper.circle(x, y, r);
var xx, yy, i, a, pathString = "";
for (i = 0; i <= n; ++i) {
a = ((4 * Math.PI * i) + (Math.PI * n) + (2 * Math.PI)) / (2 * n);
xx = x + r * Math.cos(a);
yy = y + r * Math.sin(a);
pathString += (i == 0 ? "M " : " L ") + xx + " " + yy;
}
pathString += " z";
paper.path(pathString);
</script>
</body>
</html>
重构为使用 var a,并始终具有水平基础.
Refactored to use var a, and to always have a horizontal base.
这篇关于绘制内接于圆的正多边形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!