我正在编写一个程序,我需要绘制任意数量边的多边形,每个边都由一个动态变化的给定公式进行翻译.有一些相当有趣的数学,但我被困在这个问题上.
如何只给出边数,并且理想地(但不是必须)将原点放在中心,我如何计算正多边形的顶点坐标(所有角度相等的顶点)?
例如:六边形可能有以下几点(都是floats):
( 1.5 , 0.5 *Math.Sqrt(3) )
( 0 , 1 *Math.Sqrt(3) )
(-1.5 , 0.5 *Math.Sqrt(3) )
(-1.5 , -0.5 *Math.Sqrt(3) )
( 0 , -1 *Math.Sqrt(3) )
( 1.5 , -0.5 *Math.Sqrt(3) )
Run Code Online (Sandbox Code Playgroud)
我的方法看起来像这样:
void InitPolygonVertexCoords(RegularPolygon poly)
Run Code Online (Sandbox Code Playgroud)
并且需要将坐标添加到此(或类似的东西,如列表):
Point[] _polygonVertexPoints;
Run Code Online (Sandbox Code Playgroud)
我主要对这里的算法感兴趣,但C#中的例子会很有用.我甚至不知道从哪里开始.我该如何实施呢?它甚至可能吗?!
谢谢.
我有两行:Line1和Line2.每一行是通过两个点来定义(P1L1(x1, y1), P2L1(x2, y2)和P1L1(x1, y1), P2L3(x2, y3)).我想知道这两行定义的内角.
为此,我用横坐标计算每条线的角度:
double theta1 = atan(m1) * (180.0 / PI);
double theta2 = atan(m2) * (180.0 / PI);
Run Code Online (Sandbox Code Playgroud)
在知道角度后,我计算如下:
double angle = abs(theta2 - theta1);
Run Code Online (Sandbox Code Playgroud)
我遇到的问题或疑问是:有时候我会得到正确的角度,但有时我会得到互补的角度(对于我来说).我怎么知道减去何时180º知道内角?有没有更好的算法呢?因为我尝试了一些方法:点积,下面的公式:
result = (m1 - m2) / (1.0 + (m1 * m2));
Run Code Online (Sandbox Code Playgroud)
但总是我有同样的问题; 当我有外角或内角时我才知道!
前几天我用Java编写了一个类来计算a point(X,Y)是否在多边形内部.(X和Y是double的,因为将地理坐标).
我知道,Java有类Polygon,但我不得不使用Path2D和Point2D,因为Polygon不允许double的,只是整数:(
一旦我完成了多边形Path2D,我就使用了方法contains(Path2D有了它),我的问题就解决了.
但现在,我想导入到Android,问题出在这里,因为Path2D需要导入:
import java.awt.geom.Path2D;
import java.awt.geom.Point2D;
Run Code Online (Sandbox Code Playgroud)
并且在Android中不存在awt,所以我无法使用.
那么,有没有类似于Path2D那种contains方法的类?或者我必须自己计算?
以下是我在Java中使用的方法Path2D:
private void ConstructPolygon(Vector<Point2D> coodinates)
{
this.polygon.moveTo(coodinates.get(0).getX(), coodinates.get(0).getY());
//System.out.println(coodinates.get(0).getX() + " " + coodinates.get(0).getY());
//System.out.println("asda");
for(int i = 1; i < this.num_points; i++)
{
//System.out.println(coodinates.get(i).getX() + " " + coodinates.get(i).getY());
this.polygon.lineTo(coodinates.get(i).getX(), coodinates.get(i).getY());
}
this.polygon.closePath();
}
public boolean InsideCity(Point2D …Run Code Online (Sandbox Code Playgroud)