Author Topic: Ellipse Intersect Ellipse  (Read 3937 times)

0 Members and 1 Guest are viewing this topic.

This topic contains a post which is marked as Best Answer. Press here if you would like to see it.

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Ellipse Intersect Ellipse
« on: February 13, 2020, 06:47:29 pm »
This one was a lot to chew. I think I worked it out properly on paper for the general case of any ellipse touching any other ellipse, but I had to do a mini-problem along the way to sanity-check my equations. It was pretty fun, so I thought I'd not only start this post to fill in the answer eventually, but also throw a problem.

Consider two copies of the same ellipse, except the second is rotated 90 degrees and centered on the original as shown in the screenshot. If the long length is "a", and the short length is "b", Calculate the intersection points represented by "?". To be definite: if a=10 and b=5, Calculate the exact number for "?". 100 digits of precision will do.

ellipses.png
* ellipses.png (Filesize: 7.73 KB, Dimensions: 446x441, Views: 228)
« Last Edit: February 13, 2020, 06:53:05 pm by STxAxTIC »
You're not done when it works, you're done when it's right.

Offline _vince

  • Seasoned Forum Regular
  • Posts: 422
    • View Profile
Re: Ellipse Intersect Ellipse
« Reply #1 on: February 15, 2020, 03:15:53 am »
Code: [Select]
deflng a-z
const sw = 640
const sh = 480
dim shared pi as double
pi = 4*atn(1)
declare sub circlef(x, y, r)
declare sub ellipse(x, y, rx, ry)

'screenres sw, sh, 32
screen 12


a = 280
b = 80

ellipse sw/2, sh/2, a, b
ellipse sw/2, sh/2, b, a

color 12'rgb(255,0,0)

dim r as double
dim p as double
p = a*tan(pi/4)/b
r = (a/(cos(pi/4))/sqr(1 + p*p))

circlef sw/2 + r*cos(pi/4), sh/2 - r*sin(pi/4), 10
circlef sw/2 - r*cos(pi/4), sh/2 - r*sin(pi/4), 10
circlef sw/2 - r*cos(pi/4), sh/2 + r*sin(pi/4), 10
circlef sw/2 + r*cos(pi/4), sh/2 + r*sin(pi/4), 10

sleep
system

sub circlef(x, y, r)
        x0 = r
        y0 = 0
        e = -r

        do while y0 < x0
                if e <=0 then
                        y0 = y0 + 1
                        line (x - x0, y + y0)-(x + x0, y + y0),, bf
                        line (x - x0, y - y0)-(x + x0, y - y0),, bf
                        e = e + 2*y0
                else
                        line (x - y0, y - x0)-(x + y0, y - x0),, bf
                        line (x - y0, y + x0)-(x + y0, y + x0),, bf
                        x0 = x0 - 1
                        e = e - 2*x0
                end if
        loop
        line (x - r, y)-(x + r, y),, bf
end sub

sub ellipse(x, y, rx, ry)
        a = 2*rx*rx
        b = 2*ry*ry

        x0 = rx
        y0 = 0
        xx = ry*ry*(1 - 2*rx)
        yy = rx*rx

        e = 0
        sx = b*rx
        sy = 0

        do while sx >= sy
                pset (x - x0, y + y0)
                pset (x + x0, y + y0)
                pset (x - x0, y - y0)
                pset (x + x0, y - y0)

                y0 = y0 + 1
                sy = sy + a
                e = e + yy
                yy = yy + a

                if 2*e + xx > 0 then
                        x0 = x0 - 1
                        sx = sx - b
                        e = e + xx
                        xx = xx + b
                end if
        loop

        x0 = 0
        y0 = ry
        xx = rx*ry
        yy = rx*rx*(1 - 2*ry)

        e = 0
        sx = 0
        sy = a*ry

        do while sx <= sy
                pset (x - x0, y - y0)
                pset (x + x0, y - y0)
                pset (x - x0, y + y0)
                pset (x + x0, y + y0)

                x0 = x0 + 1
                sx = sx + b
                e = e + xx
                xx = xx + b

                if 2*e + yy > 0 then
                        y0 = y0 - 1
                        sy = sy - a
                        e = e + yy
                        yy = yy + a
                end if
        loop
end sub

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Ellipse Intersect Ellipse
« Reply #2 on: February 15, 2020, 06:29:45 am »
Perfect _vince.

Here's my writeup for the solution to any ellipse touching any other ellipse in the most general 2D case - that math is explosive but when we reduce it to the example on hand, things are pretty tight:
ss1.png
* ss1.png (Filesize: 72.87 KB, Dimensions: 651x768, Views: 248)
ss2.png
* ss2.png (Filesize: 53.36 KB, Dimensions: 652x768, Views: 252)
You're not done when it works, you're done when it's right.

Marked as best answer by STxAxTIC on February 16, 2020, 04:35:27 am

Offline _vince

  • Seasoned Forum Regular
  • Posts: 422
    • View Profile
Re: Ellipse Intersect Ellipse
« Reply #3 on: February 15, 2020, 02:21:05 pm »
a cleaner expression for r
ellipser.png
* ellipser.png (Filesize: 3.27 KB, Dimensions: 364x100, Views: 225)

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Ellipse Intersect Ellipse
« Reply #4 on: February 15, 2020, 02:26:13 pm »
Isn't it even cleaner when you erase tan(pi/4)?

On that note, I thought mine was cleaner anyway! I still like it though.
You're not done when it works, you're done when it's right.

Offline _vince

  • Seasoned Forum Regular
  • Posts: 422
    • View Profile
Re: Ellipse Intersect Ellipse
« Reply #5 on: February 15, 2020, 02:43:48 pm »
I was mocking your dislike for trig!

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Ellipse Intersect Ellipse
« Reply #6 on: February 15, 2020, 02:49:58 pm »
LOL! You so got me.
You're not done when it works, you're done when it's right.