What will intersects() return in this situation? - java

There is a Rectangle2D.Double object named rect2d and a Line2D.Double object named line2d.
Please consider the following situation:
Question 1:
What will this line of code return?
boolean intersect = line2d.getBounds2D().intersects(rect2d);
Question 2:
The width/thickness of line2d is 1 pixel. It's height/length is 20 pixels.
What would be the width and height values of the Rectangle2D object returned by line2d.getBounds2D()?

It will return:
false
java.awt.geom.Rectangle2D$Double[x=10.0,y=10.0,w=0.0,h=20.0]
With this code:
Line2D.Double line2d = new Line2D.Double(10, 10, 10, 30);
Rectangle2D.Double rect2d = new Rectangle2D.Double(0, 0, 100, 100);
boolean intersect = line2d.getBounds2D().intersects(rect2d);
System.out.println(intersect);
System.out.println(line2d.getBounds2D());
Although, it's useless to say that a line is 1 pixel, because it can't be other way using Line2D.Double.
Indeed, for Java, this vertical line as a 0-pixel width boundary, so that's why it will never intersect with any other shape. With a non-vertical and non-horizontal line, its bounds intersect.
Do not use bounds to compute intersection, but directly the Shape:
boolean intersect = line2d.intersects(rect2d);
You may check RectangularShape.intersects() Javadoc for more information on how this method computes intersection.

From the docs, here and here, the first would return false and the second a bounding box at least the size of the dimensions of the line.

Related

Move a Rectangle from point to point in a ramp with changing angle

So I have this program to test the possibility of an object to slide down in a ramp given its friction, object mass and ramp angle. However I need to animate the box if the force is positive. Just a simple animation moving the box from that point to the end of the ramp. But I can't. Please help
private void drawTransform(Graphics g, double modifier) {
// redtowhite = new GradientPaint(0,0,color.RED,100, 0,color.WHITE);
Rectangle rect = new Rectangle(130,350, 350, 15);
Rectangle box = new Rectangle((int) (rect.getX()+300), 300, 50, 50);
AffineTransform at = new AffineTransform();
at.rotate(-Math.toRadians(modifier), rect.getX(), rect.getY() + rect.height);
// Transform the shape and draw it to screen
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.DARK_GRAY);
// g2d.fillRect(0, 0, 350, 600);
g2d.fill(at.createTransformedShape(rect));
g2d.draw(at.createTransformedShape(box));
}
Screenshot:
If all you want to do is move the box, this can be done by simply updating it's X position. You should be able to manipulate the rectangle's X position directly using something like "box.x++". Alternatively you could create a variable and reference that to provide the initial X co-ordinate, then updating that variable will "move" the box. One issue is this will only move the box along the X axis, hence you will also need some kind of constant downward force acting as gravity. This is easy to achieve, just minus the box's Y position value when it is not colliding with the ground, or your ramp.
Another approach is velocity based movement using vectors, however you mentioned that the animation should be simple. If you do want a smoother animation velocity based movement will provide this but you will need to perform a little research first.

Convert from java.awt.geom.Area to java.awt.Polygon

I need to convert a java.awt.geom.Area or java.awt.Shape to java.awt.Polygon. What I know about the are is: isSingular = true, isPolygonal = true. So I think a polygon shuld be able to describe the same area.
I'm not sure that it is worth converting, because Polygon is an old Java 1.0 class that can store only integer coordinates, so you might lose some precision.
Anyway, you can get a PathIterator from the Shape, and as you iterate it, add new points to a Polygon:
public static void main(String[] args) {
Area a = new Area(new Rectangle(1, 1, 5, 5));
PathIterator iterator = a.getPathIterator(null);
float[] floats = new float[6];
Polygon polygon = new Polygon();
while (!iterator.isDone()) {
int type = iterator.currentSegment(floats);
int x = (int) floats[0];
int y = (int) floats[1];
if(type != PathIterator.SEG_CLOSE) {
polygon.addPoint(x, y);
System.out.println("adding x = " + x + ", y = " + y);
}
iterator.next();
}
}
EDIT As Bill Lin commented, this code may give you a wrong polygon if the PathIterator describes multiple subpaths (for example in the case of an Area with holes). In order to take this into account, you also need to check for PathIterator.MOVETO segments, and possibly create a list of polygons.
In order to decide which polygons are holes, you could calculate the bounding box (Shape.getBounds2D()), and check which bounding box contains the other. Note that the getBounds2D API says that "there is no guarantee that the returned Rectangle2D is the smallest bounding box that encloses the Shape, only that the Shape lies entirely within the indicated Rectangle2D", but in my experience for polygonal shapes it would be the smallest, and anyway it is trivial to calculate the exact bounding box of a polygon (just find the smallest and biggest x and y coordinates).

Wrong output of intersectsLine from Line2D in Java

Why does this code:
Line2D line1 = new Line2D.Double(464.9298111721873, 103.78661133348942, 684.8391765253534, -155.44752172931908);
Line2D line2 = new Line2D.Double(414.16903384086487, 163.62456359144306, 393.52528378472925, 187.95988300984624);
line1.intersectsLine(line2);
return true?
Clearly, x coordinates of lines are way apart, and they do not intersect. I drew them onto a swing panel, and they are apart, but look like they are collinear. Is this the problem?
I tried testing on simple collinear lines (like (1, 3, 4, 3), (6, 3, 8, 3)), and it seemes to work fine.
Java Docs say that the method of the Line2D class:
public boolean intersects(double x, double y, double w, double h)
tests if the interior of the Shape intersects the interior of a specified rectangular area. It uses the Shape.intersects() method for this, however the calculations to accurately determine this intersection are very expensive.
This means that for some Shapes this method might return true even though the rectangular area does not intersect the Shape.
Although you are using the method itersectsLine(), both intersects() and intersectsLine() use the same expensive method underneath the surface. The method is called linesIntersect() and is specified here on line 298
The Area class performs more accurate computations of geometric intersection than most Shape objects and therefore can be used if a more precise answer is required. For instance:
boolean intersectionExists(Shape shape1, Shape shape2) {
Area area1 = new Area(shape1);
area1.intersect(new Area(shape2));
return !area1.isEmpty();
}
Tested using your values:
public static void main(String[] args) {
Line2D line1 = new Line2D.Double(464.9298111721873, 103.78661133348942, 684.8391765253534, -155.44752172931908);
Line2D line2 = new Line2D.Double(414.16903384086487, 163.62456359144306, 393.52528378472925, 187.95988300984624);
System.out.println("Lines intersect? " + intersectionExists(line1, line2));
}
Output:
Lines intersect? false

java.awt.Rectangle. intersection()

I was developing an task when I decided to use java.awt.Rectangle to calculate the intersection between two rectangles.
I realised that the output is different from what I expected. I'm not sure if I understood how this method works or not.
For the values in the example here
java.awt.Rectangle[x=0,y=10,width=5,height=8]
java.awt.Rectangle[x=3,y=15,width=17,height=14]
I expect the intersection to be java.awt.Rectangle[x=3,y=10,width=2,height=8] but the program prints java.awt.Rectangle[x=3,y=15,width=2,height=3] instead!
here is my code:
public void printIntersection(){
Rectangle r1 = new Rectangle(0, 10, 5, 8);
Rectangle r2 = new Rectangle(3, 15, 17, 14);
Rectangle r3 = r1.intersection(r2);
System.out.println(r1);
System.out.println(r2);
System.out.println(r3);
}
Can anyone help me by pointing out what am I missing here?
UPDATE:
The source of my confusion is that the code treats the (x,y) values in the constructor as the bottom-left corner, while the class doc suggests that they are the upper-left corner!
The answer you are getting is correct.
The method works like this.
1st Rectangle:
X co-ordinates: 0
Y co-ordinates: 10
Width: 5
Height: 8
2nd Rectangle:
X co-ordinates: 3
Y co-ordinates: 15
Width: 17
Height: 14
For the intersection the X and Y co-ordinates are same as 2nd rectangle. Width is 5-3=2 and Height is 18-15=3
I also had trouble with this. The way I think about it is that the grid used is inverted on the y axis. Because point 0.0 is at the top left of the screen with point 0,1 being below rather than above that point you can get the answer you are expecting by inverting the the y axis in your original code.
For example.
public void printIntersection(){
Rectangle r1 = new Rectangle(0, 10 * -1 , 5, 8);
Rectangle r2 = new Rectangle(3, 15 * -1, 17, 14);
Rectangle r3 = r1.intersection(r2);
System.out.println(r1);
System.out.println(r2);
System.out.println(r3);
}
This should give you the answer you are expecting
The opposite corners of your rectangles are (0,10),(5,18) and (3,15),(20,29), so the intersection is (3,15),(5,18), so I think the result is the expected one. Notice the opposite corners of the resultant one are the bottom-right of the first one and the top-left of the second one.
Edit: The way it works is: the starting point is (x,y), and the sides are calculated adding the widthand height to the starting point, so the opposite corner will be (x+width,y+height)
Final note: (0,0) is the upper-left corner of the canvas:
Here is an example: (0,0,4,4) and (2,2,4,4) intersection is (2,2,2,2): (2,2) is the upper-left one and (2+2,2+2) is opposite corner

Plotting functions with using java GUI

I need to plot a polynomial given a start point and an end point. I've looked at using the paincomponent to do do it, but I'm getting confused because the origin is in the top left corner and I need my grid lines to be dynamic.
How do I make it so I can create a dynamic scale and what java class is best used for plotting a function?
For example if the user were to enter -2 2 x^2 the plotter would need to create a grid that goes from -2 has 0 in the middle and 2 on the right for the x coordinates. However if the user enters x^3 we have negative coordinates in the y plane and I'll need to slide the line containing the x coordinates up to accomidate.
Thanks!
The way I think you could do this(and I may be wrong) is to draw the y-line and x-line of your graph, and label the values for x and y using drawString at the correct position.
To illustrate this I did this example for you:
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
Point2D.Double from = new Point2D.Double(300, 100);
Point2D.Double to = new Point2D.Double(300, 300);
Line2D.Double line = new Line2D.Double(from, to);
g2.draw(line);
Point2D.Double from2 = new Point2D.Double(150, 200);
Point2D.Double to2 = new Point2D.Double(450, 200);
Line2D.Double line2 = new Line2D.Double(from2, to2);
g2.draw(line2);
g2.drawString("-1", 290, 215);
}
The drawString method would draw "-1" below 0. So you could use the same thing to draw -2 below -1, etc.
I know this sounds inefficient, but it's the only way I can think of.
Hope someone else will have a better solution for you.
It shouldn't be too difficult to transform the y-coordinate if you know the size of area you want to paint to. But to be honest, why don't you use one of the plotting libs like JFreeChart, ...?

Categories

Resources