I use QuadCurve2D (Quadratic Curve Segment) in Java. Can I get y-coordinate by x-coordinate from this object?
Something like this:
QuadCurve2D q = new QuadCurve2D.Float ();
q.setCurve (x1, y1, ctrlx, ctrly, x2, y2);
int X = 100;
int Y = q.getY (X); // (?)
Related
I am currently trying my hands on a GameEngine and later on a Game and want to create an abstract class Hitbox, which gets extended by RectHitbox. Now when I create a Polygon Object, which I want to use to keep track of the shape, it also asks me for number of points npoints. I looked at this documentation but I don't know if "total number of points" refers to just the corner points or ALL points inside the area aswell.
Now my question is: is there is any advantage to automatically calculate the parameter npoints or if there is a disadvantage to leaving it Null.
What I mean by automatically calculate npoints (if it refers to the corner points):
public Hitbox(int x1, int y1, int x2, int y2){
xpoints[0] = x1;
xpoints[1] = x2;
ypoints[0] = y1;
ypoints[1] = y2;
polygon = new Polygon(xpoints, ypoints, xpoints.length+ypoints.length);
}
and what I mean by leaving it null:
public Hitbox(int x1, int y1, int x2, int y2){
xpoints[0] = x1;
xpoints[1] = x2;
ypoints[0] = y1;
ypoints[1] = y2;
polygon = new Polygon(xpoints, ypoints, null);
}
I'm trying to generate an astroid (Not asteroid) graphic similar to this:
This graphic output is generated on a 400x400 DrawingPanel. And the math is based on making curves from straight lines. However, I'm having a problem with my x,y coordinates. instead of making the astroid, I'm generating this:
Here is the code:
import java.awt.*;
public class GraphicsProject
{
//main method
public static void main (String[]args){
//Initialize the variable
//The Window is 400 x 400 pixels in size
DrawingPanel panel = new DrawingPanel (400,400);
Graphics g = panel.getGraphics();
drawAstroid(g,0,0, 400);
}
public static void drawAstroid(Graphics g, int x, int y, int size){
int scale = size/40;
int x1, x2,x3, x4, y1, y2, y3, y4;
y1 = y;
y2 = y + size;
y3 = y + 1/2 * size;
y4 = y + 1/2 * size;
x1 = x + 1/2 * size;
x2 = x + 1/2 * size;
x3 = x + 1/2 * size;
x4 = x + 1/2 * size;
for (int i = 0; i < 21; i++ ){
//Draws Upper Left
g.drawLine(x4 - scale * i, y4, x1, y1 + scale * i);
//Draws Upper Right
g.drawLine(x4 + scale * i, y4, x2, y2 - scale * i);
//Draws Lower Left
g.drawLine(x3 - scale * i, y3, x4, y4 + scale * i);
//Draws Lower Right
g.drawLine(x3 + scale * i, y3, x2, y2 - scale * i);
}
}
}
Any insight into how to resolve this would be appreciated.
I'm trying to print out an array of 10 objects. For some reason though, when I print out the array, there are anywhere from 15 to 22 elements in the array. I can't figure out why. Can someone please point me in the right direction?
import java.util.Random;
public class Main {
public static void main( String[] args ) {
Shape[] myShapes = new Shape[10]; //Array of 10 shapes
Random rand = new Random(); //Random number generator
int shape, x1, y1, x2, y2, x3, y3;
double radius;
for( int i = 0; i < 10; i++ ) {
shape = rand.nextInt(3); //randomly select shape
switch( shape ) {
case 0: //Triangle
x1 = rand.nextInt(101);
y1 = rand.nextInt(101);
x2 = rand.nextInt(101);
y2 = rand.nextInt(101);
x3 = rand.nextInt(101);
y3 = rand.nextInt(101);
myShapes[i] = new Triangle( x1, y1, x2, y2, x3, y3 );
System.out.println("Triangle: " + myShapes[i].area());
case 1: //Rectangle
x1 = rand.nextInt(101);
y1 = rand.nextInt(101);
x2 = rand.nextInt(101);
y2 = rand.nextInt(101);
myShapes[i] = new Rectangle( x1, y1, x2, y2 );
System.out.println("Rectangle: " + myShapes[i].area());
case 2: //Circle
radius = rand.nextDouble()*100.0;
x1 = rand.nextInt(101);
y1 = rand.nextInt(101);
myShapes[i] = new Circle( radius, x1, y1 );
System.out.println("Circle: " + myShapes[i].area());
}
}
}
Can you please use break; for each case?
Your array actually has 10 elements. But it puts content in each elements up to three times - because control continues from case to case. Thus, if case 0 is right, it will put three shapes and print three prints. If case 1 is right, it will put two shapes and print two prints.
If you put break after each case, then on each iteration it will just put one shape and print just once.
I have a ellipse with a mid-point 'mid' an a horizontal radius 'h' and a vertical radius 'v' and a Line2D.
Now I need some code to calculate the two intersection points of the two.
I already tried some code and tried it on my own but there always is a mistake.
Does somebody have some working code?
You will need to use algebra to solve the two equations, and it will get a little bit messy. First you have to write out the ellipse
(1) (x/h)^2 + (y/v)^2 = 1
and the line in the format
(2) y = ax + b
First, shift over your coordinate axis so the ellipse is centered on the origin. You can do that by subtracting mid from the line. Once you have calculated the points of intersection, shift them back by adding mid.
You can calculate the linear slope from delta-y/delta-x from the starting and ending points of the line. You will have to check if the slope is vertical. If the slope is vertical, you just have to check whether or not the x-value of the line points falls in the location of the ellipse, and then easily calculate the values. Draw it out on paper and see how to calculate it.
Assume now that the slope is not vertical. Since you know y in terms of x from the line, square that and substitute into (1). Simplifying gives a quadratic equation.
(3) ((ah)^2+v^2)x^2 + (2abh^2)x + ((hb)^2-(hv)^2) = 0
Using the quadratic formula gives the two values for the x coordinates of the intersection. If there are two real values for x, there are two intersections. If there is only one real solution for x, there is one intersection. If there are no real solutions for x, there is no intersection.
Given ax^2
+ bx + c = 0, x is given by
x = (1/2a)(-b +- Sqrt(b^2 - 4ac))
Let D = b^2 - 4ac
If D < 0, there are no intersections
If D = 0, there is one intersection
If D > 0, there are two intersections
Once you have calculated the values of the x intersection, substitute the values of x into (2) to get the y values.
Now, you need to make sure that these points fall within the line. To do this, just check that the x and y components of the calculated points satisfy x1 <= x <= x2 and y1 <= y <= y2 where x1 is the smallest and x2 the largest x-endpoint of the line, and y1 is the smallest and y2 the largest y-endpoint of the line.
Here is an example method that I made
public static ArrayList<Point2D> getIntersection(double x1, double x2, double y1, double y2, double midX, double midY, double h, double v) {
ArrayList<Point2D> points = new ArrayList();
x1 -= midX;
y1 -= midY;
x2 -= midX;
y2 -= midY;
if (x1 == x2) {
double y = (v/h)*Math.sqrt(h*h-x1*x1);
if (Math.min(y1, y2) <= y && y <= Math.max(y1, y2)) {
points.add(new Point2D(x1+midX, y+midY);
}
if (Math.min(y1, y2) <= -y && -y <= Math.max(y1, y2)) {
points.add(newPoint2D(x1+midX, -y+midY);
}
}
else {
double a = (y2 - y1) / (x2 - x1);
double b = (y1 - a*x1);
double r = a*a*h*h + v*v;
double s = 2*a*b*h*h;
double t = h*h*b*b - h*h*v*v;
double d = s*s - 4*r*t;
if (d > 0) {
double xi1 = (-s+Math.sqrt(d))/(2*r);
double xi2 = (-s-Math.sqrt(d))/(2*r);
double yi1 = a*xi1+b;
double yi2 = a*xi2+b;
if (isPointInLine(x1, x2, y1, y2, xi1, yi1)) {
points.add(new Point2D.Double(xi1+midX, yi1+midY);
}
if (isPointInLine(x1, x2, y1, y2, xi2, yi2)) {
points.add(new Point2D.Double(xi2+midX, yi2+midY);
}
}
else if (d == 0) {
double xi = -s/(2*r);
double yi = a*xi+b;
if (isPointInLine(x1, x2, y1, y2, xi, yi)) {
points.add(new Point2D.Double(xi+midX, yi+midY));
}
}
}
return points;
}
public static boolean isPointInLine(double x1, double x2, double y1, double y2, double px, double py) {
double xMin = Math.min(x1, x2);
double xMax = Math.max(x1, x2);
double yMin = Math.min(y1, y2);
double yMax = Math.max(y1, y2);
return (xMin <= px && px <= xMax) && (yMin <= py && py <= yMax);
}
Feel free to check my algebra and my code, but you should solve this problem by carefully going through each algebraic step.
I would use the built in ellipse and line/polygon classes there they both have methods to determine collisions and intersection
When the line is given by two points P0 and P1, any point along the line is (X, Y) = (X0, Y0) + t (X1 - X0, Y1 - Y0) = (X0, Y0) + t (DX, DY).
The ellipse is (X - Xm)²/h² + (Y - Ym)²/v² = 1.
We will use a trick to simplify computation: take all X, subtract Xm and divide by h, giving x; take all Y, subtract Ym and divide by h, giving y. This will turn the ellipse into a circle centered at the origin. (You can do all computations without this reduction of the coordinates if you prefer.)
Now, (x, y) = (x0, y0) + t (dx, dy) and x² + y² = 1.
Or (t dx + x0)² + (t dy + y0)² = 1.
Or (dx² + dy²) t² + 2 (dx x0 + dy y0) t + (x0² + y0² - 1) = 0.
Solve this second degree equation for t. If there are real roots, you can check if they belong to the line segment by the condition 0 <= t <= 1. The intersections themselves are given by the first equation.
I need to cover some polygon with rectangles here's an example :
The black figure in a black square is the polygon that i need to cover with those green rectangles but i need to do it more efficiently that just make a net like i did. Because as you can see there can be place more green rectangles if i moved them.
Rectangles inside are fixed size(just not as big as polygon it self), one for all like in the picture, they can be places vertically and horizontally, i want to fill the polygon as much as it can fit it inside of it, this polygon is just for example, there can be different polygons with holes in them for example that black small square is a hole.
module = rectangle
private void coverWithModules(Graphics g, int[] xpoints, int[] ypoints) {
Polygon module;
int x1, x2, x3, x4, y1, y2, y3, y4;
int moduleRowNumber = 0;
int totalRows = (getMax(ypoints) / moduleHeight);
while (moduleRowNumber < totalRows) {
// first module
x1 = getMin(xpoints);
y1 = getMin(ypoints) + distance * moduleRowNumber + moduleHeight
* moduleRowNumber;
x2 = x1 + moduleWidth;
y2 = y1;
x3 = x1 + moduleWidth;
y3 = y1 + moduleHeight;
x4 = x1;
y4 = y1 + moduleHeight;
int[] x = { x1, x2, x3, x4 };
int[] y = { y1, y2, y3, y4 };
module = new Polygon();
// check if point are inside the polygon
checkModulePlacement(g, x, y, module);
// placing modules in a row
while (x1 < getMax(xpoints)) {
x1 = x2 + distance;
y1 = getMin(ypoints) + distance * moduleRowNumber
+ moduleHeight * moduleRowNumber;
x2 = x1 + moduleWidth;
y2 = y1;
x3 = x1 + moduleWidth;
y3 = y1 + moduleHeight;
x4 = x1;
y4 = y1 + moduleHeight;
int[] xx = { x1, x2, x3, x4 };
int[] yy = { y1, y2, y3, y4 };
module = new Polygon();
checkModulePlacement(g, xx, yy, module);
}
moduleRowNumber++;
}
}
private void checkModulePlacement(Graphics g, int[] x, int[] y, Polygon module) {
boolean pointInside = true;
boolean pointOnObstraction = true;
for (int i = 0; i < x.length; i++) {
if (pointInside) {
pointInside = roof.contains(x[i], y[i]);
}
module.addPoint(x[i], y[i]);
}
pointOnObstraction = checkForObstractions(module);
g.setColor(Color.GREEN);
if (pointInside == true && pointOnObstraction == false ) {
g.drawPolygon(module);
}
}
I was looking for something and i have found Something like this maybe there is more stuff like this ?
I don't know where to search for such info. What should i look up to get what i need ? Maybe there is some kind of library for this kind of things ?