0

With:

getrectangle().intersects(circle.getBounds2D())

I can see if my rectangle was touched by my circle. That works also.

But now I have to set, that if it touches the left or right side (no matter where there) of the rectangle, I have to print a System.out.println.

And if it touches the upper or lower side of the rectangle (no matter where there) of the rectangle, I have to output another System.out.println. Now how do I do this?

ALso how do I check which side it hit?

fhwui3
  • 1
  • `Shape.getBounds2D` returns the bounding box of the `Shape`. So your rectangle may be intersecting the bounding box but not the actual circle. `Shape.contains(x,y)` returns true if `(x.y)` is within or on the perimeter of a shape. But `bounds.contains(p)` does not imply `shape.contains(p).` So your initial assumption of `getrectangle().intersects(circle.getBounds2D()) works` may not be true. To answer your specific question about `where` the `rectangle` intersects the `circle` requires more information about your specific implementation and may require some algebra and/or trig. – WJS May 02 '22 at 17:24
  • You already got an answer in your last question. If you have further questions then update the other question so everybody knows what has already been suggested and all the information relating to the question is found in one place. – camickr May 02 '22 at 18:24
  • Please don't post the same question. Just edit (change title and content) and save. – WJS May 02 '22 at 19:00
  • Thank you WJS, but our teacher said to us that we need to check that the bounding boxes touch, so because of this i thought that would be correct. And about the implementation, we have a 2d ellipse and a rectangle. So i dont know really how can i check with algebra that ther touch annother. (The rectangle has the dimension 60x60 pixel) @WJS – fhwui3 May 02 '22 at 19:43

1 Answers1

0

Here's the way you can do this

Take the angle (in radians) of the circle's direction, and convert it to x and y values using Math.asin and Math.acos. You need to divide one by the other to get into y=ax + b; Then you need to convert your rectangle's edges into lines (similar to above, figure out the angle the sides are on using the corner points and Math.sin and Math.cos)

... Sounds easy, right???

Now you have two lines and all you have to do is solve the equation for when they are equal

ControlAltDel
  • 32,042
  • 9
  • 48
  • 75
  • sorry what? For my its sound not easy hahahaha. – fhwui3 May 02 '22 at 19:41
  • How can i get the angle of a 2d ellipse – fhwui3 May 02 '22 at 19:41
  • If you have a reasonable math background in both algebra and trigonometry it is not hard. Otherwise, it will probably be daunting. – WJS May 02 '22 at 20:56
  • *How can i get the angle of a 2d ellipse.* You said you had a circle. And there is no one angle since angles are relative to some position. If I told you a circle is an ellipse with `excentricity of zero` would that help? Imho, there is actually too much to explain and I doubt your instructor would have required this type of solution without the proper math prerequisite. I recommend you discuss the assignment with him/her to see what is expected. – WJS May 02 '22 at 21:20