Ok
I have the line and the component. The component itself is a JPanel. I draw shapes inside it. And adding the shape to the container. This shape can be any polygon. And drawing a curved line (this can be Line2D, CubicCurve2D or QuadCurve2D) in a container.
Now I need to draw an arrow shape on the container so that while moving a line this arrow would span around the shape.
There can be many curve lines connected to the center of the shape with different Control point. And the shape can be any regular polygon.
At least I need the intersection points of the rounded rectangle and the curve line below image.
Edit: I want to determine the intersection points of a curved line and an arbitrary regular polygon. Given polygon width, height, center points and the starting, ending and the control points of the line.
Edit: I can't post questions so I will edit this one. How can I zoom both JComponents and Graphics object? If it should done with AffineTransform then how should I transform event points on JComponents?
You can use BasicStroke and getStrokedShape(). Set thickness=e.g. 5 and get stroked shapes of the Polygon and Line. Then create 2 Area classes based on the stoked shapes and get intersection Shape.
Related
I've been trying to draw a simple figure, however I'm having issues drawing arms. Whenever I use drawRect it just draws a horizontal or a straight rectangle.
I've only tried drawRect since drawLine is not thick enough.
I'm expecting a diagonal rect or maybe a thicker line that could potentially look like an arm.
I'm working on a project, and I need to be able to detect collisions between circles. I already found a mathematical formula for that : http://cgp.wikidot.com/circle-to-circle-collision-detection
But I've got a question, how can I detect if there is a rectangle in this area ? Or just a part of a rectangle inside ?
I've got : coordinates of the center of a circle and the radius, and for the rectanble I've got a x and y coordinate, and width an height. I guess that x and y are just a point and with that I'm able to guess the form with the width and the height.
Any idea ?
Thanks a lot !
Write a method to check whether a point lies within a circle or not.
Call that method for all corner points of the rectangle (calculated from x, y, width and height) on both circles.
Use your existing circle intersection detector method to prune calls.
Hope this helps.
Good luck.
You can use java.awt.geom.Area class.
It has a constructor to create an Area from Shape
public Area(Shape s)
So create simple areas for your circles and rectangle. Then you can either combine areas using the methods to obtain new areas.
public void add(Area rhs)
public void subtract(Area rhs)
And check whether area intersects or contains another area via
public void intersect(Area rhs)
public boolean contains(Rectangle2D r)
This sounds like a variation of the technique described in this answer.
The same two cases apply (either the circle's centre is in the region, or one or more of the rectangle's edges intersects the region)... the difference is that instead of considering the circle in general, you need to consider the intersection of the circle.
The first case is easy because you can swap centre points. If the rectangle's centre point is in the intersection of the circles, then the rectangle is partly inside. This is easy to determine: find the centre point of the rectangle, see if it's in the first circle, see if it's in the second circle.
The second case is complicated, because it requires you to calculate the curves where the circles intersect. If the edges of the rectangle intersect either of those curves then the rectangle overlaps the intersection. As a special case, if one circle lies completely inside the other one, then the line to check is the border of the smaller circle.
If you don't need an exact answer, then the second case can be approximated. First, find the points where the two circles intersect (or use the method you've already come up with, if you can). These two points can be used to construct a bounding rectangle (they are either the top left/bottom right or top right/bottom left points of a rectangle). If this bounding rectangle intersects with your rectangle, then your rectangle probably overlaps the circle intersection.
All in all, this is fairly complicated if you want to an exact answer that works properly with all of the special cases (one circle completely inside the other, the rectangle intersects both circles but not their intersection, etc). I hope this helps a little.
A library I've used before called the JTS topology suite might be appropriate for your needs. It's orientated more towards GIS operations than pure euclidean geometry, but it can easily do all of these calculations for you once you've got the shapes defined:
import com.vividsolutions.jts.util.*
GeometricShapeFactory gsf = new GeometricShapeFactory();
gsf.setSize(100);
gsf.setNumPoints(100);
gsf.setBase(new Coordinate(100, 100));
//configure the circle as appropriate
Polygon circleA = gsf.createCircle();
//configure again and create a separate circle
Polygon circleB = gsf.createCircle();
//configure a rectangle this time
Polygon rectangle = gsf.createRectangle();
Geometry circleIntersection = circleA.intersection(circleB);
return rectangle.intersects(circleIntersection);
I am writing a simple game, or so it seemed, that has a functionality of rotating a Rectangle as the mouse moves. This did not seem like a problem at first but it just became a problem. What the Rectangle should do is rotate around a point as the mouse moves around on the Panel.
If you look at the picture, when the mouse moves the Rectangle will rotate. I know you can use the rotate functionality with Graphics2D.
g2d.rotate(angle, centerx, centery);
This is not very help full because I can not get the coordinates of the moving rectangle. This rotates the full graphics! How will I be able to draw this Rectangle so that it does this. I don't have an idea as on how to start. Please help.
Some more code and context would be nice, but based on the current question: You could create a transformed shape. Particularly, Rectangle and Rectangle2D implement the Shape interface. And you can create an AffineTransform that represents the rotation that you are currently doing to your Graphics. So the relevant part of the code should roughly look like
Rectangle2D rectangle = ...
AffineTransform at = AffineTransform.getRotateInstance(
angle, centerx, centery);
Shape rotatedRectangle = at.createTransformedShape(rectangle);
g2d.draw(rotatedRectangle);
You mentioned "collision" in the title. If you intend to use this rectangle in some sort of collision detection, you should note that it is not directly possible to intersect two arbitrary Shape objects. Particulary, you can not intersect the Shape rotatedRectangle with another Shape otherRotatedRectangle, but only with a Rectangle otherRectangle. If this is an issue, you have several options, but this would rather fit into a dedicated question.
In my game the player uses a Rectangle for his bounding box, he can do this because I only need to rotate the player image and not the acctual rectangle but for a eletric beam that one of the bosses have I need to use a Shape because I need to rotate the acctual bounding box and not only the image. The problem is that because I have one Rectangle and one Shape I can't use rectangle.intersect(shape) nor rectangle.intersect((Rectangle)shape) so how would I be able to check the collision between the rectangle and the shape? or is there a way that I can rotate a rectangle on instead of a shape (I use the createTransformedShape instance of an AffineTransform to rotate the shape )?
I'm working with Graphics2D(java)
I'm trying move a random shape with mouse drag.
This random shape is stored in Area object of java.awt.geom.Area class.
I know how to select that area object, I just need to figure out how to actually move it to the new coordinates.
Shapes like ellipse and rectangle can be easily moved but how to move shapes like polygon or shapes that contain curve and no specific predefined structure.
In order to draw a rectangle using the Graphics class, you need the origin point and the width and height.
In order to draw an ellipse using the Graphics class, you need the origin point and the width and height.
Each of these simple figures is made up of an origin point and the dimensions of the figure.
Similarly, for any complex shape, you need an origin point. It's the origin point that "moves" when you move the shape with a mouse drag. In other words, the origin of the mouse move corresponds with the origin of the figure. As the mouse moves to new X and y coordinates, your origin moves to new x and y coordinates.
Let's say a rectangle has an origin of 10, 10. Let's say the origin of the mouse drag is 30, 30. As the mouse moves to new x and y coordinates, you change the origin of the rectangle. In this example, when the mouse has moved to 40, 40, the origin of the rectangle becomes 20, 20.
The rectangle and ellipse already have a draw method in the Graphics class. You'll probably have to write your own draw method for the complex shape.