Zeynel
Zeynel

Reputation: 13515

How to connect two ellipses from the perimeter?

I am practicing working with vectors. In this sketch I draw a line connecting centers of two ellipses. How can I shorten the line so that it touches the perimeter of each ellipse (not the center)?

PVector v1, v2;

void setup(){
  noLoop();
  v1 = new PVector(40, 20);
  v2 = new PVector(25, 50);
}

void draw(){
  ellipse(v1.x, v1.y, 12, 12);
  ellipse(v2.x, v2.y, 12, 12);

  line(v1.x, v1.y, v2.x, v2.y);
}

Upvotes: 1

Views: 336

Answers (1)

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324640

First you'd need to calculate the points where the line would cross the edges of the circles. Fortunately, this is quite easy: (Note that I don't know Processing, so treat this as psuedocode)

direction = atan2(v2.y-v1.y,v2.x-v1.x)
x1 = v1.x+cos(direction)*radius
y1 = v1.y+sin(direction)*radius
x2 = v2.x-cos(direction)*radius
y2 = v2.y-sin(direction)*radius

Then just draw the line (x1,y1,x2,y2)

Upvotes: 2

Related Questions