Email or username:

Password:

Forgot your password?
Devine Lu Linvega

Someone taught me how to approx distance between two points without sqrt.

max(abs(x),abs(y)) + min(abs(x),abs(y))/2

git.sr.ht/~rabbits/decadv/tree

12 comments
phoebos

@neauoire your formula only refers to one point (x,y) ?

Devine Lu Linvega

Thanks so much to the @lovebyteparty community for the help with this #lovebytetcc.

Devine Lu Linvega

I learnt how to do transition between colors today, went back and revisited yesterday's demo and added a little dither effect that softens the edges.

DHeadshot's Alt

@neauoire
In a 4-colour system, that's very impressive!

Lovebyte Demoparty

@neauoire Everything about this is just super cool, the shape, the patterns, the fact that it's sqrt-less... and the mix with the music is just ๐Ÿ‘ฉโ€๐Ÿณ๐Ÿ’‹!

๐ŸŒˆ Andrew โ˜„๏ธ

@neauoire Nifty! I've been doing an approximate `sqrt(n)` recently with this iterative approach:
a = n
for 1..m:
a = (a + n / a) / 2

So I tried this using your approach for an initial guess:
a = max(abs(x),abs(y)) + min(abs(x),abs(y))/2;
s = x*x + y*y;
if (a != 0.0) {
a = (a + s / a) / 2;
a = (a + s / a) / 2;
}

Within a range of -100 to 100 this takes your approximation from a max error of about 34% to a max error of 0.4%. The average error goes from 9% to 0.4%.

It's still just 7% max error and 0.4% average error if you only do one iteration.

Sorry, no fancy visualisations, but hereโ€™s a picture of them both overlayed (I considered a โ€œthey're the same picture" meme instead):

@neauoire Nifty! I've been doing an approximate `sqrt(n)` recently with this iterative approach:
a = n
for 1..m:
a = (a + n / a) / 2

So I tried this using your approach for an initial guess:
a = max(abs(x),abs(y)) + min(abs(x),abs(y))/2;
s = x*x + y*y;
if (a != 0.0) {
a = (a + s / a) / 2;
a = (a + s / a) / 2;
}

A graph of concentric circles, thereโ€™s nothing to indicate itโ€™s two graphs overlayed.
johnfredcee

@neauoire Things Iโ€™d wish Iโ€™d known 30 years ago..

Go Up