我是新来的,所以如果我做错了,请原谅。我已经对我的主题进行了研究,但我似乎无法找到如何正确准备递归三角形。
我正在尝试接受一个我能够做的论点,并创建一个我可以用Java做的三角形。 当argument = 1时,应创建一个三角形。 在argument = 2中,应创建四个三角形。 在那之后我的程序搞砸了,我很难弄清楚如何在三个三角形的中点出现三个三角形。我之前见过的一些建议建议我使用中点但我不能发现双方应该有多长时间。
以下是代码:
public class Sierpinski{
public static void main(String args[]) {
String firstArgument = args[0];
int b = Integer.parseInt( firstArgument );
int c = b - 1;
double rt = Math.sqrt(3);
double lol = rt / 4;
double[] x = {.25, .5, .75};
double[] y = {lol, 0, lol};
double x1 = .25;
double x2 = .5;
double x3 = .75;
double y1 = lol;
double y2 = 0;
double y3 = lol;
StdDraw.filledPolygon(x,y);
//Small Triangles
while ( c > 0 ){
//Small Triangle 1
double xb1 = (x1) / 2 / c;
double yb1 = (lol) / 2 / c;
double xb2 = (x1) / c;
double yb2 = (y2) / 2;
double xb3 = (x1 + x2) / 2 / c;
//double yb30 = (.44301270189) / 2 * 2 * i;
double xb4 = 1 - ((x1) / 2 / c);
double yb4 = ((lol) / 2 / c);
double xb5 = 1 - ((x1) / c);
double yb5 = (0) / 2;
double xb6 = 1 - ((x1 + x2) / 2 / c);
double xb7 = x2 + ((x1) / 2 / c);
double yb7 = ((lol) / 2 / c);
double xb8 = x2 + ((x1) / c);
double yb8 = (0) / 2;
double xb9 = x2 + ((x1 + x2) / 2 / c);
double xb10 = .125 + ((x1) / 2 / c);
double yb10 = (lol) / 2 / c;
double xb11 = (x1) / c;
double yb11 = (0) / 2;
double xb12 = (x1 + x2) / 2 / c;
double[] xb0 = {xb1, xb2, xb3, xb4, xb5, xb6, xb7, xb8, xb9};
double[] yb0 = {yb1, yb2, yb1, yb4, yb5, yb4, yb7, yb8, yb7};
StdDraw.filledPolygon(xb0,yb0);
//Small Triangle 2
double xc1 = (.25 + .5) / 2;
double yc1 = 1 - ( 0.44301270189 - .27409407929) * 2 ;
double xc2 = (.5); // 2;
double yc2 = (.44301270189); // 2;
double xc3 = (.5 + .75) / 2;
//double yb30 = (.44301270189); //2 * b;
double xc4 = ((.25 + .5) / 2);
double yc4 = .5 + (1 - ( 0.44301270189 - .27409407929) * 2) ;
double xc5 = (.5); // 2;
double yc5 = .5 + (.44301270189); // 2;
double xc6 = (.5 + .75) / 2;
double xc7 = x2 + ((x1) / 2 / c);
double yc7 = ((lol) / 2 / c);
double xc8 = x2 + ((x1) / c);
double yc8 = (0) / 2;
double xc9 = x2 + ((x1 + x2) / 2 / c);
double xc10 = .125 + ((x1) / 2 / c);
double yc10 = (lol) / 2 / c;
double xc11 = (x1) / c;
double yc11 = (0) / 2;
double x12 = (x1 + x2) / 2 / c;
double[] xc0 = {xc1, xc2, xc3};
double[] yc0 = {yc1, yc2, yc1};
StdDraw.filledPolygon(xc0,yc0);
//Small Triangle 3
double xd1 = (.5 + .75) / 2;
double yd1 = (.44301270189) / 2;
double xd2 = (.75); // 2;
double yd2 = (0) / 2;
double xd3 = (.25 + .5) / 2 + .5;
double yd30 = (.44301270189) /2;
double[] xd0 = {xd1, xd2, xd3};
double[] yd0 = {yd1, yd2, yd1};
StdDraw.filledPolygon(xd0,yd0);
c--;
}
}
}
现在当它在3处形成时它完全得到前四个,第五个重叠(我假设是因为我试图使用确切的数字,因为我看到其他没有重叠问题,但那是另一个问题,好像飞过我的脑袋。)
答案 0 :(得分:0)
我在这里猜测很多,但是假设你想找到原始三角形每个边缘的中点,并使用中点和初始三角形顶点在原始三角形中刻上4个三角形,递归地将它们带到一个任意深度。
为此,您的代码将是这样的。
public class TestIt {
public static void inscribeTriangles(double xa, double ya,
double xb, double yb,
double xc, double yc, int depth) {
if (depth <= 1) {
// We've reached the bottom of the recursion, so draw!
// Don't fill it or you won't see the inscribed triangles.
StdDraw.polygon(new double[]{xa, xb, xc}, new double[]{ya, yb, yc});
} else {
// Else recur to draw 4 inscribed triangles.
double xab = (xa + xb) / 2, yab = (ya + yb) / 2;
double xbc = (xb + xc) / 2, ybc = (yb + yc) / 2;
double xca = (xc + xa) / 2, yca = (yc + ya) / 2;
inscribeTriangles(xa, ya, xab, yab, xca, yca, depth - 1);
inscribeTriangles(xb, yb, xbc, ybc, xab, yab, depth - 1);
inscribeTriangles(xc, yc, xca, yca, xbc, ybc, depth - 1);
// For Sierpinksi, don't subdivide 4th triangle:
// inscribeTriangles(xab, yab, xbc, ybc, xca, yca, depth - 1);
// And in this case you can also fill the polygon if you like.
}
}
public static void main(String [] args) {
inscribeTriangles(0, 0, 1, 0, 0.5, 1, 5);
}
}
这是经过轻微测试但应该没问题。注意我的起始三角形不是等边的,但算法并不需要它。
答案 1 :(得分:0)
这是完整的工作代码。享受:)
public class voicehovich{
int generations;
double borderSideSize;
public static void main(String args[]) {
voicehovich v =new voicehovich();
String firstArg = args[0];
v.generations = Integer.parseInt( firstArg );
double startX=0.25;
double startY=0.5;
double startSide=0.5;
//stop basing on side size
v.borderSideSize = startSide/ (Math.pow(2,v.generations-1));
//start creating triangles
v.drawTriangle(startX,startY, startSide);
}
public void drawTriangle(double topLeftX, double topLeftY, double sideSize){
double[] x = {topLeftX, topLeftX+sideSize/2, topLeftX+sideSize};
double lowY = Math.sqrt(sideSize*sideSize-((sideSize/2)*(sideSize/2)));
double[] y = {topLeftY, topLeftY-lowY, topLeftY};
StdDraw.filledPolygon(x,y);
if (sideSize>borderSideSize){
findCoordinatesOfThree(topLeftX,topLeftY,sideSize);
}
}
public void findCoordinatesOfThree(double topLeftX, double topLeftY, double sideSize){
//clculate coordinate of 3 triangles
double tLeftX = topLeftX+sideSize/4-sideSize/2;
double tLeftY = topLeftY-Math.sqrt(sideSize*sideSize-((sideSize/2)*(sideSize/2)))/2 ;
double tRightX = topLeftX+sideSize*3/4;
double tRightY = topLeftY-Math.sqrt(sideSize*sideSize-((sideSize/2)*(sideSize/2)))/2 ;
double tTopX = topLeftX+sideSize/4;
double tTopY = topLeftY+Math.sqrt(sideSize/2*sideSize/2-((sideSize/4)*(sideSize/4)));
drawTriangle( tLeftX, tLeftY, sideSize/2);
drawTriangle( tRightX, tRightY, sideSize/2);
drawTriangle( tTopX, tTopY, sideSize/2);
}
}