0

I need to draw a spider using the Graphics package. The problem though is that its size, number of legs and eyes are specified by the user.

I've got a few questions:

  1. how can I randomly select a point on a circle so I can draw a line (for legs) from there while keeping in mind that drawLine(), for instance, takes only integer arguments?
  2. how can I randomly select a point inside the circle used as a center of an eye so that the circle (eye) fits within the ranges of an outer circle (body)?
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
David
  • 125
  • 2
  • 15
  • 3
    *"1) how can I randomly select a point on a circle so I can draw a line"* - That's basic trig (I say that with tounge in cheek) [for example](http://stackoverflow.com/questions/25923480/simple-circle-rotation-simulate-motion/25923780#25923780) – MadProgrammer Apr 20 '16 at 22:14
  • *"for legs) from there while keeping in mind that drawLine(), for instance, takes only integer arguments?"* which is basically irrelevant, you can cast `double` to `int` or use `Math.round` or use `Line2D` instead, which takes `double` or `float` values – MadProgrammer Apr 20 '16 at 22:15
  • *"2) how can I randomly select a point inside the circle used as a center of an eye so that the circle (eye) fits within the ranges of an outer circle (body)?*" - Becomes a little more complicated, but `Ellipse2D` has `intersects` and `contains` functionality which can be used to make "some" determinations; Remember, you already have the center point and radius/diameter of the main circle, if you point (plus the eyes radius) is does not fit within these parameters, then you will need to find a new spot – MadProgrammer Apr 20 '16 at 22:18
  • Apparently it's to early in the morning and I shouldn't be allowed to operate a keyboard :P – MadProgrammer Apr 20 '16 at 22:35
  • Start with [`CircleTest(8)`](http://stackoverflow.com/a/2510048/230513). – trashgod Apr 20 '16 at 23:59

2 Answers2

3

Selecting a point on a circle just requires getting a random angle. Java uses radians for it's trigonometric functions so a random double between 0 and 1 is multiplied by 2π.

Random r = new Random();
double angle = r.nextDouble() * Math.PI * 2;

Drawing legs is simple trigonometry which requires finding the x and y of each line. For this sine and cosine functions are used. The line can then be drawn off the center point of the circle (centerX and centerY), ending at a specified length in pixels (legLength).

The process can be repeated to draw multiple legs with a specified offset (legOffset) and repeated and offset again (by π) to draw legs on the other side.

for (int i = 0; i < 4; i++) {
    int lineX = (int) radius * Math.cos(angle);
    int lineY = (int) radius * Math.sin(angle));

    g.drawLine(circleX + lineX , circleY + lineY , circleX + lineX * legLength, circleY + lineY * legLength);
    angle += legOffset;
}

Drawing the eyes is essentially the same process as the legs. Each eye can drawn at a specified angle and distance from the center of the circle.

int eyeX = (int) distance * Math.cos(angle);
int eyeY = (int) distance * Math.sin(angle));
g.fillOval(eyeX - eyeRadius, eyeY - eyeRadius, eyeRadius* 2, eyeRadius* 2);
Betato
  • 108
  • 8
1

The easiest way to get random integers is to create an instance of Random and with random.nextInt(bound) you get an integer between 0 (inclusive) and bound (exclusive), [0, bound).

Instead of selecting the upper left corner of the spider, I would randomly select the center of the spider and then draw everything in relation to it.

Now let's define the radius r = size / 2.

  1. Selecting a random point with insuring that the spider is fully visible:

    x = r + random.nextInt(width - 2 * r);
    y = r + random.nextInt(height - 2 * r);
    
  2. Drawing the body with a diameter of r and not 2r to ensure the legs are visible: g.fillOval(x - r / 2, y - r / 2, r, r);

  3. Drawing the legs and eyes: There are numerous strategies, you could draw lines from the center with length r for the legs and very small circles at distance r/4 from the center for the eyes. After selecting an initial random angle, you can use the golden angle to calculate the position of the next leg / eye, this ensures they are never drawn at the same positon (https://en.wikipedia.org/wiki/Golden_angle).

Note: draw the legs first, then the body and the eyes last.

maraca
  • 8,468
  • 3
  • 23
  • 45