这部分是一个数学问题,部分是一个java实现问题。
我正在尝试使用Java Graphics中的fillPolygon(Polygon p)
方法绘制三角形。
有一个类Triangle.java
,它扩展了Polygon并采用了两个参数:Point location
和int height
。 height参数的目的是轻松调整三角形的高度,然后通过求解30,60,90三角形来计算边长。
public class Triangle extends Polygon {
private final static int numberOfSides = 3;
// Class creates a polygon for use in fillPolygon() method call in
// Piece.java
public Triangle(Point p, int height) {
// Pass in the hieght of the desired triangle and the side lengths will
// be calculated by solving a 30, 60, 90 triangle
super(new int[] { p.x,
(p.x + ((int) ((2 * height) / Math.sqrt(3.0)) / 2)),
p.x + (int) ((2 * height) / Math.sqrt(3.0)) }, new int[] {
(p.y + (int) ((2 * height) / Math.sqrt(3.0))), p.y,
(p.y + (int) ((2 * height) / Math.sqrt(3.0))) }, numberOfSides);
}
我需要做的是创建一个同心三角形的方法,无论三角形的大小或位置如何。 显然,内三角形的椎体必须与外三角形的所有顶点等距(如果外三角形是等边的,则特别容易)。三角形之间的差距是无关紧要的,因为我希望可以作为参数传递。
就像我说这主要是一个数学问题,虽然我还没有找到一个与同心三角形一般等距的方程。
感谢您的高级帮助。
编辑:我解决了这个问题。我不得不重做我接近三角形的方法(使用Path2D.Double而不是Polygon以获得更好的准确性),但它确实有效。我就这样做了:
根据@ajb的建议,我计算了等边三角形的Circumcenter(与其他质心相同),然后计算三角形的坐标,用于Path2D.Double:
public class Triangle extends Path2D.Double {
private double[] xCoordinates, yCoordinates, innerXCoordinates,
innerYCoordinates;
private Point location;
private double height, circumcenterRadius, sideLength;
public Triangle(Point p, double h) {
if (p != null && h > 0) {
this.location = p;
this.height = h;
this.circumcenterRadius = (2.0 / 3.0) * (this.height);
this.sideLength = (2 * this.height)/Math.sqrt(3.0);
}
// Set values for the x coordiantes using location and height
this.xCoordinates = new double[] { p.x,
p.x + sideLength/ 2,
p.x + sideLength };
// Set values for the x coordinates using location and height
this.yCoordinates = new double[] { p.y + this.height, p.y,
p.y + this.height };
}
然后使用正确的坐标,迭代然后将它们添加到路径:
public void constructPath() {
// Sets the path to the first coordinates of the triangle (bottom left).
this.moveTo(this.xCoordinates[0], this.yCoordinates[0]);
// Iterates through the coordinates and adds them to the path
for (int i = 1; i < this.xCoordinates.length; i++) {
this.lineTo(this.xCoordinates[i], this.yCoordinates[i]);
}
// Closes the path and compeletes the Polygona
this.closePath();
}
然后我创建了一个方法,它将在Circumcenter Radii中获得所需的差异(以确定缩小外接三角形的程度)。使用它,我计算了x和y偏移量,以便重新定位内三角形,使其正确地刻在外三角形内部
public Path2D constructInscribedTriangle(double radiusDifference) {
// Calculate the new circumcenterRadius of the inscribed Triangle
double innerCircumcenterRadius = (this.circumcenterRadius - radiusDifference);
double innerHeight = (3.0 / 2.0) * innerCircumcenterRadius;
double innerSideLength = (2 * innerHeight)/Math.sqrt(3.0);
double xOffset = (radiusDifference/2) * (Math.sqrt(3.0));
// Recalculate coordintes for inner Triangle
this.innerXCoordinates = new double[] {
(this.location.x + xOffset),
(this.location.x + xOffset)
+ innerSideLength/ 2,
(this.location.x + xOffset)
+ innerSideLength };
this.innerYCoordinates = new double[] {
(this.location.y + radiusDifference) + innerHeight,
(this.location.y + radiusDifference),
(this.location.y + radiusDifference) + innerHeight };
Path2D.Double inscribedTriangle = new Path2D.Double();
inscribedTriangle.moveTo(this.innerXCoordinates[0],
this.innerYCoordinates[0]);
for (int h = 1; h < this.innerXCoordinates.length; h++) {
inscribedTriangle.lineTo(this.innerXCoordinates[h],
this.innerYCoordinates[h]);
}
inscribedTriangle.closePath();
return inscribedTriangle;
}