Now i draw pictures in circle by formula :
float x = CIRCLE_RADIUS * (float) Math.sin(2f * Math.PI * drawSquareIndex / ITEMS_COUNT + angle) * 1.75f;
where x - is a X point of circle item.
And i have a circle.
but i want to draw pictures on ellipse. What the formula i need to use?
How i can do that?
P.S. sorry about quality. Make a question from phone.
You can use parametric ellipse equaition (a = b is a case of cirle):
x = a * cos(t)
y = b * sin(t)
t = 0..2*PI
In your case
// Pseudo code
for (double t = 0; t < 2 * PI; t += 0.001) { // <- or different step
double x = RadiusX * Math.Cos(t);
double y = RadiusY * Math.Sin(t);
Paint(x, y);
}
Related
Include details about your goal:
So first of all, my goal is to make the points rotations of a cube aroud X, Y, and Z axis to rotate the cube itself.
Describe expected:
I expect to have that after an 17.18° rotation around X axis:
I expect to have that after an 17.18° rotation around Z axis:
Actual results:
Black shape is the default cube with no rotations
White shape is the rotated cube.
My actual result after an 17.18° rotation around X axis with my formula:
My actual result after an 17.18° rotation around Z axis with my formula:
Show what you’ve tried and tell us what you found (on this site or elsewhere) and why it didn’t meet your needs. You can get better answers when you provide research.
I have saw a few site that describe exactly what I want:
https://www.desmos.com/calculator/imazvy40oz?lang=fr
https://fr.khanacademy.org/computer-programming/cube-rotating-with-user-interaction/5953495622746112
Show some code
First of all i am using Java because my problem is my Minecraft virtually shape don't meet with the real one;
Rotation methods
public void rotateX() {
for(int i = 0; i < points.size(); i++) {
Vector vector = points.get(i);
double sinTheta = Math.sin(eulerAngle.getX());
double cosTheta = Math.cos(eulerAngle.getX());
double y = vector.getY();
double z = vector.getZ();
vector.setY(y * cosTheta - z * sinTheta);
vector.setZ(z * cosTheta + y * sinTheta);
}
}
public void rotateY() {
for(int i = 0; i < points.size(); i++) {
Vector vector = points.get(i);
double sinTheta = Math.sin(eulerAngle.getY());
double cosTheta = Math.cos(eulerAngle.getY());
double x = vector.getX();
double z = vector.getZ();
vector.setX(x * cosTheta - z * sinTheta);
vector.setZ(z * cosTheta + x * sinTheta);
}
}
public void rotateZ() {
for(int i = 0; i < points.size(); i++) {
Vector vector = points.get(i);
double sinTheta = Math.sin(eulerAngle.getZ());
double cosTheta = Math.cos(eulerAngle.getZ());
double x = vector.getX();
double y = vector.getY();
vector.setX(x * cosTheta - y * sinTheta);
vector.setY(y * cosTheta + x * sinTheta);
}
}
My setup method
public void setupPoints() {
ArrayList<Vector> points = new ArrayList<Vector>();
points.add(getA());
points.add(getB());
points.add(getC());
points.add(getD());
points.add(getE());
points.add(getF());
points.add(getG());
points.add(getH());
this.points = points;
rotateZ();
rotateY();
rotateX();
}
My points locations are (i know its not an exact cube):
My enviroment:
note: Only Y rotation works perfectly, i noticed that the rotation on the Y axis corresponds to that of the Z axis in a coordinate system where Y would be the "height", its the first time that i work on 3d space plan so i am learning. Thanks if you read until here.
Your rotation methods are correct, but they rotate around axes that pass through the origin.
You are expecting them to rotate around an axis that passes through the center of the object. You don't see the problem with the rotation around Y axis because it happens to pass through the center of the cube.
You can fix this by "moving" the object to the origin before rotating, and moving it back after rotating. For example, for the X rotation:
double y = vector.getY() - objectCenterY;
double z = vector.getZ() - objectCenterZ;
vector.setY(y * cosTheta - z * sinTheta + objectCenterY);
vector.setZ(z * cosTheta + y * sinTheta + objectCenterZ);
I have a color circle where the user can choose a color from. The color is calculated with this method
public int getColorForPoint(int x, int y, float[] hsv) {
x -= fullCircleRadius;
y -= fullCircleRadius;
double centerDist = Math.sqrt(x * x + y * y);
hsv[0] = (float) (Math.atan2(y, x) / Math.PI * 180f) + 180;
hsv[1] = Math.max(0f, Math.min(1f, (float) (centerDist / innerCircleRadius)));
return Color.HSVToColor(hsv);
}
Now I need the reversed method to calculate the x and y coordinate by a given color (hsv array).
To be more specific: The user can save a color and the indicator in the color circle should "jump" to the saved color on the circle.
But I'm quite lost with this mathematics.
Looking at the way you calculate centerDist - I can tell your circle centre is at the origin (0,0).
Basically HSV is a polar co ordinate, all you need is to convert a polar co ordinate to cartesian co ordinate. which is done as follows.
public double[] getHSVtoCartesian(double[] hsv) {
double [] xy;
double theta = hsv[0];
double r = hsv[1];
xy[0] = r * Math.cos(theta);
xy[1] = r * Math.sin(theta);
return xy;
}
I'm new at openGL 4.5 and I currently working with lights and it's all okay when I calculate vertex normals during the geometry drawing, but now I have to calculate face's normal for a cone and I have no idea on how to do it. I'm drawing it with GL_TRIANGLE_STRIP. We can see the current snippet below:
float coneAngle = (float) Math.atan(radius / height);
coneCos = (float) Math.cos(coneAngle);
coneSin = (float) Math.sin(coneAngle);
// circleResolution * heightResolution * 3
for(int i = 0; i <= circleResolution; i++)
{
for(int j = 0; j <= heightResolution; j++)
{
angle = 2 * ((float) Math.PI) * (i / (float) circleResolution);
cos = ((float) Math.cos(angle));
sin = ((float) Math.sin(angle));
x = radius * cos;
z = radius * sin;
// Cone Bottom
vertices.add(x);
vertices.add(0.0f);
vertices.add(z);
// Cone Bottom Normal
vertices.add(coneCos * cos);
vertices.add(coneSin);
vertices.add(coneCos * sin);
// Cone Top
vertices.add(0f);
vertices.add(height);
vertices.add(0f);
// Cone Top Normal
vertices.add(0f);
vertices.add(0f);
vertices.add(0f);
}
}
// Center of Bottom Circle - Vertices
vertices.add(0.0f);
vertices.add(0.0f);
vertices.add(0.0f);
// Center of Bottom Circle - Normal
vertices.add(0.0f);
vertices.add(-1.0f);
vertices.add(0.0f);
// CircleResolution - Bottom Circle - TRIANGLE_FAN
for (int j = 0; j <= circleResolution; j++)
{
angle = (2 * ((float) Math.PI)) * ( j / (float) circleResolution );
x = (float) Math.cos(angle);
z = (float) Math.sin(angle);
vertices.add(radius * x);
vertices.add(0.0f);
vertices.add(radius * z);
// Normal
vertices.add(0.0f);
vertices.add(-1.0f);
vertices.add(0.0f);
}
Polygon That Approximates a Cone
The normal vector to a line or (vetor) can be achiefed by a 90° rotation of a vector along the line.
A vector along the flank of the cone is (radius, -height). The normal vector is (-(-height), radius).
In your case the normal vector attribute of the flank of the cone, for the bottom ("Cone Bottom Normal") and top ("Cone Top Normal") vertices is:
// lenght of the flank of the cone
float flank_len = Math.sqrt(radius*radius + height*height);
// unit vector along the flank of the cone
float cone_x = radius / flank_len;
float cone_y = -height / flank_len;
.....
// Cone Bottom Normal
vertices.add(-cone_y * cos);
vertices.add( cone_x );
vertices.add(-cone_y * sin);
.....
// Cone Top Normal
vertices.add(-cone_y * cos);
vertices.add( cone_x );
vertices.add(-cone_y * sin);
I created a function for drawing a circle in oepnGL java, and I want to rotate another circle on the circumference of a circle ?
This is my function for create circle, how to change it for drawing the circle on circumference?
For example create a new circle using as center coordinates points from first circle ?
private void rotateAroundOz(GL2 gl, int r, double cx, double cy) {
int step = 1;
gl.glLineWidth(5);
gl.glBegin(GL.GL_LINE_LOOP);
for (int i=0; i<360; i+=step) {
gl.glColor3d(1, 0, 0);
gl.glVertex2d(cx + r * Math.cos(Math.toRadians(i)), cy + r * Math.sin(Math.toRadians(i)));
}
gl.glEnd();
}
You just have to draw a circle (with rotateAroundOz()) using a position that you compute from
cx + r * Math.cos(Math.toRadians(i)), cy + r * Math.sin(Math.toRadians(i))
// This is the attributes of the invisible circle: "PositionCircle"
//that will gives you the circumference
float positionCircle_Radius = 1.0;
float positionCircle_CenterX = 0.0;
float positionCircle_CenterY = 0.0;
// This is actually the circle that you want to draw from the
// "PositionCircle"
int positionOnCircumferenceInDegrees = 90;
float drawnCircle_Radius = 2.0;
float drawnCircle_CenterX = positionCircle_CenterX + positionCircle_Radius * Math.cos(Math.toRadians(positionOnCircumferenceInDegrees));
float drawnCircle_CenterY = positionCircle_CenterY + positionCircle_Radius * Math.sin(Math.toRadians(positionOnCircumferenceInDegrees));
rotateAroundOz(gl, drawnCircle_Radius, drawnCircle_CenterX, drawnCircle_CenterY)
So you can add positionOnCircumferenceInDegrees and drawnCircle_Radius as parameters for your new function.
(That's my first response ever on SO :p hope it's comprehensible!)
How to get coordinates of a point in a coordinate system when all I have is the origin coordinates (x, y) and the angle from the origin to the point and the distance from the origin to the point?
You use Math.cos, Math.sin like this:
pointX = x + distance * Math.cos(angle)
pointY = y + distance * Math.sin(angle)
Note about radians / degrees
Math.cos and Math.sin assumes the argument is given in radians (0…2π). If you have the angle in degrees (0…360), you would use Math.cos(Math.toRadians(angle)) for instance.
If r is the distance from origin and a is the angle (in radians) between x-axis and the point you can easily calculate the coordinates with a conversion from polar coordinates:
x = r*cos(a)
y = r*sin(a)
(this assumes that origin is placed at (0,0), otherwise you should add the displacement to the final result).
The inverse result is made by computing the modulo of the vector (since a distance + angle make a vector) and the arctangent, which can be calculated by using the atan2 funcion.
r = sqrt(x*2+y*2)
a = atan2(y,x)
If d is the distance and A is the angle, than the coordnates of the point will be
(x+d*Cos(A), y+ d*Sin(A))
px = x + r * cos(phi)
py = y + r * sin(phi)
where [px py] is the point you are searching for, [x y] is the "origin", r is the distance and phi is the angle to the target from the origin.
EDIT: http://en.wikipedia.org/wiki/Polar_coordinate_system This link which was helpfully posted by Bart Kiers could yield some background information.
Short answer
// math equations
pointX = distance * cos(angle) + x
pointY = distance * sin(angle) + y
// java code [angle in radian]
double pointX = distance * Math.cos(Math.toRadians(angle)) + x;
double pointY = distance * Math.sin(Math.toRadians(angle)) + y;
Detailed answer
As per below diagram
// finding pointX let's start by
cos(angle) = (pointX - x) / distance
distance * cos(angle) = (pointX - x)
(pointX - x) = distance * cos(angle)
pointX = distance * cos(angle) + x
// finding pointY let's start by
sin(angle) = (pointY - y) / distance
distance * sin(angle) = (pointY - y)
(pointY - y) = distance * sin(angle)
pointY = distance * sin(angle) + y