我把这个伪代码正确地翻译成Java了吗?

我把这个伪代码正确地翻译成Java了吗?,java,math,Java,Math,几天前我在Math.se上问过,得到了以下伪代码的答案: Function RandomCircleInside(centerX, centerY, radius): Let newRadius = radius * Random() Let radians = 2.0 * 3.14159265358979323846 * Random() Let deviation = (radius - newRadius) * Sqrt(Random()) Let

几天前我在Math.se上问过,得到了以下伪代码的答案:

Function RandomCircleInside(centerX, centerY, radius):
    Let  newRadius = radius * Random()
    Let  radians = 2.0 * 3.14159265358979323846 * Random()
    Let  deviation = (radius - newRadius) * Sqrt(Random())
    Let  newX = centerX + deviation * Cos(radians)
    Let  newY = centerY + deviation * Sin(radians)
    Return (newX, newY, newRadius)
End Function
我将伪代码更改为Java,并添加了自己的更改以满足我的需要。新代码如下所示:

Circle createNewCircle(int centerX, int centerY, int radius, int newR, Color newColor) {
    int newRadius = radius * Random();
    double radians = 2.0 * 3.141592653589793 * Random();
    double deviation = (radius - newRadius) * Math.sqrt(Random());
    System.out.println(radius + " - " + newRadius + " * sqrt(0 or 1) = " + (radius-newRadius) + " * (0 or 1) = " + deviation);
    double newX = centerX + deviation * Math.cos(radians);
    System.out.println(centerX + " + " + deviation + " * cos(" + radians + ") = " + (centerX + deviation) + " * " + Math.cos(radians));
    double newY = centerY + deviation * Math.sin(radians);
    int newCirX = (int) newX;
    int newCirY = (int) newY;
    Circle newCir = new Circle(newCirX, newCirY, newR*2, newR*2, newR, newColor, true);
    return newCir;
}
    import java.awt.Color;
import java.awt.Graphics;

public class Circle {

    public int X, Y, Width, Height, radius;
    public Color color;
    public boolean toFill;

    public Circle(int x, int y, int width, int height, int radius, Color color, boolean fill) {
        X = x;
        Y = y;
        Width = width;
        Height = height;
        this.radius = radius;
        this.color = color;
        toFill = fill;
    }

    public void render(Graphics g) {
        g.setColor(color);
        for(int i=-5; i<5; i++) {
            if(toFill) {
                g.fillOval(X+i, Y+i, Width-i, Height-i);
            } else {
                g.drawOval(X+i, Y+i, Width-i, Height-i);
            }
        }
    }

    public boolean contains(Circle pBound) {
        int pBoundCenterX = pBound.X+pBound.radius;
        int cirCenterX = X+radius;

        int diffBetweenCentersX = Math.abs(pBoundCenterX-cirCenterX);

        int pBoundCenterY = pBound.Y+pBound.radius;
        int cirCenterY = Y+radius;

        int diffBetweenCentersY = Math.abs(pBoundCenterY-cirCenterY);
        if(diffBetweenCentersX<= (pBound.radius+radius) && diffBetweenCentersX>=Math.abs(pBound.radius-radius)) { // X
            if(diffBetweenCentersY>=Math.abs(pBound.radius-radius)) { // Y
                return true;
            }
        }

        return false;
    }

    public int getX() {
        return X;
    }

    public int getWidth() {
        return Width;
    }

    public int getRadius() {
        return radius;
    }

    public void setWidth(int width) {
        Width = width;
    }

    public int getHeight() {
        return Height;
    }

    public void setHeight(int height) {
        Height = height;
    }

    public void setX(int x) {
        X = x;
    }

    public int getY() {
        return Y;
    }

    public void setY(int y) {
        Y = y;
    }
}
代码本身应该在一个先前存在的圆内创建一个新的圆。我创建了一个circle类,如下所示:

Circle createNewCircle(int centerX, int centerY, int radius, int newR, Color newColor) {
    int newRadius = radius * Random();
    double radians = 2.0 * 3.141592653589793 * Random();
    double deviation = (radius - newRadius) * Math.sqrt(Random());
    System.out.println(radius + " - " + newRadius + " * sqrt(0 or 1) = " + (radius-newRadius) + " * (0 or 1) = " + deviation);
    double newX = centerX + deviation * Math.cos(radians);
    System.out.println(centerX + " + " + deviation + " * cos(" + radians + ") = " + (centerX + deviation) + " * " + Math.cos(radians));
    double newY = centerY + deviation * Math.sin(radians);
    int newCirX = (int) newX;
    int newCirY = (int) newY;
    Circle newCir = new Circle(newCirX, newCirY, newR*2, newR*2, newR, newColor, true);
    return newCir;
}
    import java.awt.Color;
import java.awt.Graphics;

public class Circle {

    public int X, Y, Width, Height, radius;
    public Color color;
    public boolean toFill;

    public Circle(int x, int y, int width, int height, int radius, Color color, boolean fill) {
        X = x;
        Y = y;
        Width = width;
        Height = height;
        this.radius = radius;
        this.color = color;
        toFill = fill;
    }

    public void render(Graphics g) {
        g.setColor(color);
        for(int i=-5; i<5; i++) {
            if(toFill) {
                g.fillOval(X+i, Y+i, Width-i, Height-i);
            } else {
                g.drawOval(X+i, Y+i, Width-i, Height-i);
            }
        }
    }

    public boolean contains(Circle pBound) {
        int pBoundCenterX = pBound.X+pBound.radius;
        int cirCenterX = X+radius;

        int diffBetweenCentersX = Math.abs(pBoundCenterX-cirCenterX);

        int pBoundCenterY = pBound.Y+pBound.radius;
        int cirCenterY = Y+radius;

        int diffBetweenCentersY = Math.abs(pBoundCenterY-cirCenterY);
        if(diffBetweenCentersX<= (pBound.radius+radius) && diffBetweenCentersX>=Math.abs(pBound.radius-radius)) { // X
            if(diffBetweenCentersY>=Math.abs(pBound.radius-radius)) { // Y
                return true;
            }
        }

        return false;
    }

    public int getX() {
        return X;
    }

    public int getWidth() {
        return Width;
    }

    public int getRadius() {
        return radius;
    }

    public void setWidth(int width) {
        Width = width;
    }

    public int getHeight() {
        return Height;
    }

    public void setHeight(int height) {
        Height = height;
    }

    public void setX(int x) {
        X = x;
    }

    public int getY() {
        return Y;
    }

    public void setY(int y) {
        Y = y;
    }
}
其中,
cir1
是先前存在的圆,
cir
是新的圆

有什么我没有正确编码的吗?我尝试了几种不同的变体,但它们都给出了相同的结果

在我实现伪代码之前,我的圆圈是这样的:

Circle createNewCircle(int centerX, int centerY, int radius, int newR, Color newColor) {
    int newRadius = radius * Random();
    double radians = 2.0 * 3.141592653589793 * Random();
    double deviation = (radius - newRadius) * Math.sqrt(Random());
    System.out.println(radius + " - " + newRadius + " * sqrt(0 or 1) = " + (radius-newRadius) + " * (0 or 1) = " + deviation);
    double newX = centerX + deviation * Math.cos(radians);
    System.out.println(centerX + " + " + deviation + " * cos(" + radians + ") = " + (centerX + deviation) + " * " + Math.cos(radians));
    double newY = centerY + deviation * Math.sin(radians);
    int newCirX = (int) newX;
    int newCirY = (int) newY;
    Circle newCir = new Circle(newCirX, newCirY, newR*2, newR*2, newR, newColor, true);
    return newCir;
}
    import java.awt.Color;
import java.awt.Graphics;

public class Circle {

    public int X, Y, Width, Height, radius;
    public Color color;
    public boolean toFill;

    public Circle(int x, int y, int width, int height, int radius, Color color, boolean fill) {
        X = x;
        Y = y;
        Width = width;
        Height = height;
        this.radius = radius;
        this.color = color;
        toFill = fill;
    }

    public void render(Graphics g) {
        g.setColor(color);
        for(int i=-5; i<5; i++) {
            if(toFill) {
                g.fillOval(X+i, Y+i, Width-i, Height-i);
            } else {
                g.drawOval(X+i, Y+i, Width-i, Height-i);
            }
        }
    }

    public boolean contains(Circle pBound) {
        int pBoundCenterX = pBound.X+pBound.radius;
        int cirCenterX = X+radius;

        int diffBetweenCentersX = Math.abs(pBoundCenterX-cirCenterX);

        int pBoundCenterY = pBound.Y+pBound.radius;
        int cirCenterY = Y+radius;

        int diffBetweenCentersY = Math.abs(pBoundCenterY-cirCenterY);
        if(diffBetweenCentersX<= (pBound.radius+radius) && diffBetweenCentersX>=Math.abs(pBound.radius-radius)) { // X
            if(diffBetweenCentersY>=Math.abs(pBound.radius-radius)) { // Y
                return true;
            }
        }

        return false;
    }

    public int getX() {
        return X;
    }

    public int getWidth() {
        return Width;
    }

    public int getRadius() {
        return radius;
    }

    public void setWidth(int width) {
        Width = width;
    }

    public int getHeight() {
        return Height;
    }

    public void setHeight(int height) {
        Height = height;
    }

    public void setX(int x) {
        X = x;
    }

    public int getY() {
        return Y;
    }

    public void setY(int y) {
        Y = y;
    }
}

但现在看起来是这样的:

Circle createNewCircle(int centerX, int centerY, int radius, int newR, Color newColor) {
    int newRadius = radius * Random();
    double radians = 2.0 * 3.141592653589793 * Random();
    double deviation = (radius - newRadius) * Math.sqrt(Random());
    System.out.println(radius + " - " + newRadius + " * sqrt(0 or 1) = " + (radius-newRadius) + " * (0 or 1) = " + deviation);
    double newX = centerX + deviation * Math.cos(radians);
    System.out.println(centerX + " + " + deviation + " * cos(" + radians + ") = " + (centerX + deviation) + " * " + Math.cos(radians));
    double newY = centerY + deviation * Math.sin(radians);
    int newCirX = (int) newX;
    int newCirY = (int) newY;
    Circle newCir = new Circle(newCirX, newCirY, newR*2, newR*2, newR, newColor, true);
    return newCir;
}
    import java.awt.Color;
import java.awt.Graphics;

public class Circle {

    public int X, Y, Width, Height, radius;
    public Color color;
    public boolean toFill;

    public Circle(int x, int y, int width, int height, int radius, Color color, boolean fill) {
        X = x;
        Y = y;
        Width = width;
        Height = height;
        this.radius = radius;
        this.color = color;
        toFill = fill;
    }

    public void render(Graphics g) {
        g.setColor(color);
        for(int i=-5; i<5; i++) {
            if(toFill) {
                g.fillOval(X+i, Y+i, Width-i, Height-i);
            } else {
                g.drawOval(X+i, Y+i, Width-i, Height-i);
            }
        }
    }

    public boolean contains(Circle pBound) {
        int pBoundCenterX = pBound.X+pBound.radius;
        int cirCenterX = X+radius;

        int diffBetweenCentersX = Math.abs(pBoundCenterX-cirCenterX);

        int pBoundCenterY = pBound.Y+pBound.radius;
        int cirCenterY = Y+radius;

        int diffBetweenCentersY = Math.abs(pBoundCenterY-cirCenterY);
        if(diffBetweenCentersX<= (pBound.radius+radius) && diffBetweenCentersX>=Math.abs(pBound.radius-radius)) { // X
            if(diffBetweenCentersY>=Math.abs(pBound.radius-radius)) { // Y
                return true;
            }
        }

        return false;
    }

    public int getX() {
        return X;
    }

    public int getWidth() {
        return Width;
    }

    public int getRadius() {
        return radius;
    }

    public void setWidth(int width) {
        Width = width;
    }

    public int getHeight() {
        return Height;
    }

    public void setHeight(int height) {
        Height = height;
    }

    public void setX(int x) {
        X = x;
    }

    public int getY() {
        return Y;
    }

    public void setY(int y) {
        Y = y;
    }
}


我的所有代码都可以在github上找到:

我认为您的代码中存在一些问题

1.首先,不清楚为什么您的
圆有
半径
宽度
高度
。对于一个圆,这三件事应该是一样的。另外,如果
toFill
true
时,您的
渲染
看起来很奇怪。这是一个简化版本(注意:我没有编译它,所以可能有一些bug):

可能应该是这样的

 int newCirX = (int) newX - newRadius;
 int newCirY = (int) newY - newRadius;
看起来你搞砸了中间人对左上角。事实上,我认为你犯了这样一个错误,这是一个支持重命名我在第1项中建议的
x
y
的论点