简体   繁体   中英

How do I get the coordinates of a diagonal circle?

I am almost done of getting the coordinates of a diagonal circle.

Here is what I have so far.

// Center point
double centerX;
double centerY;
double centerZ;
for (double degree = 0D; degree < 360D; degree = degree + 8D) {
    double angle = degree * Math.PI / 180D;
    // Difference from the center
    double x = 1.5D * Math.cos(angle);
    double y;
    if (degree >= 0D && degree < 90D) {
        y = degree / 90D;
    } else if (degree >= 90D && degree < 180D) {
        y = 1D - ((degree - 90D) / 90D);
    } else if (degree >= 180D && degree < 270D) {
        y = -1D * ((degree - 180D) / 90D);
    } else {
        y = -1D * (1D - ((degree - 270D) / 90D));
    }
    double z = 1.5D * Math.sin(angle);
    // New point
    double pointX = centerX + x;
    double pointY = centerY + y;
    double pointZ = centerZ + z;
}

Here is the output in a game. It is not perfect because it creates some edges and it looks inefficient to me.

How do I correct it? Is there a better way to do this?

This should look similar to what you have already, but it's simpler and smoother:

double y = 1.0D * Math.sin(angle);

Now, with these dimensions, the result is not quite a circle, but a stretched ellipse. If you want a circle, make sure the coefficients on the cosine and sine obey the Pythagorean Theorem . For example:

double x = 1.5D * Math.cos(angle);
double y = 0.9D * Math.sin(angle);
double z = 1.2D * Math.sin(angle);

These coefficients will ensure that x^2 + y^2 + z^2 is a constant for every angle. You can verify that this is true, given the identity cos^2 + sin^2 = 1. (The coefficient representing the hypotenuse should be attached to the coordinate that uses a different trig function than the other two.)

For the most maintainable code, you might find it better to assign (x, y, z) = (cos, sin, 0) and then apply a rotation matrix, or a sequence of rotation matrices, to the vector (x, y, z). This will be easier to read and harder to mess up, if you want to fine-tune the amount of rotation later.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM