0

My maths isn't that good so I'm having a bit of trouble in one of my applications that I'm trying to do where I want a rectangle to represent a vehicle and I want that vehicle/rectangle to "drive" around in a circle. Imagine a roundabout with only 1 vehicle in it, just circling around forever.

If I can get some help how to do that then I'll be able to build on the example and most importantly learn.

If someone could write up a simple example for me I'd be grateful. No background no images, just a rectangle "driving" around in a circle. I'm using java and Swing.

Force444
  • 3,321
  • 9
  • 39
  • 77

3 Answers3

2

Sorry, I am not sure if could understand clear you exactly need. If you need to draw rectangle which is moving around inside of circle, you can use sin/cos functions.

Something like that:

double r = 50.0; // radius (it might radius of your circle, but consider dimensions of rectangle to make sure you are drawing inside of circle, e.g. circleRadius - rectangeDimesion / 2.0)
for (int f = 0; f < 360; f++) {
    double x = Math.sin(Math.toRadians((double)f)) * r;
    double y = Math.cos(Math.toRadians((double)f)) * r;
    // draw rectangle on [x, y] coordinates
}
Nikolay Antipov
  • 920
  • 2
  • 8
  • 17
1

If you know the radius of the round about, all the you would need would be a trigonometric function and the angle which the vehicle makes to the round about. You could take a look at this simple introduction which should get you started in the right direction.

On another hand, another approach would be to use a Transformation Matrix where you start with a matrix containing two points (your X and Y co-ordinates) and you transform them to become the new co-ordinates.

You can then rotate the rectangle to mimic a vehicle turning.

If you have a limited background in Mathematics, the first option might be easier for you to grasp.

npinti
  • 51,780
  • 5
  • 72
  • 96
1

This is more an extended comment than an answer.

I would divide the problem up into several easier problems, and work on each of them separately:

  • Draw your rectangle with a specified center location and long axis orientation.
  • Determine the center point and long axis orientation for an object orbiting around the origin. Note that to get make the long axis a tangent it needs to be perpendicular to the radius through the center.
  • Translate the whole system so that it orbits the desired point, rather than the origin.
Patricia Shanahan
  • 25,849
  • 4
  • 38
  • 75