How do I get the coordinates of a diagonal circle?

I'm almost ready to get the coordinates of the diagonal circle.

Here's 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 result in the game. It's not perfect because it creates some edges and it looks inefficient to me.

How to fix it? Is there a better way to do this?

+3


source to share


1 answer


It should look similar to what you already have, but simpler and smoother:

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

      

Now, with these dimensions, the result is not exactly a circle, but a stretched ellipse. If you need a circle, make sure that the cosine and sine coefficients match 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 constant for each angle. You can check that this is true given the identity cos ^ 2 + sin ^ 2 = 1. (The coefficient representing the hypotenuse must be anchored to a coordinate that uses a different trigger function than the other two.)

For the most convenient code, you might be better off assigning (x, y, z) = (cos, sin, 0) and then applying a rotation matrix or 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 RPM later.

+2


source







All Articles