[solved] if x2+y2 <= 1 then z2=1-(x2+y2) : z=sqr(z2) ?

General discussion for topics related to the FreeBASIC project or its community.
Post Reply
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

[solved] if x2+y2 <= 1 then z2=1-(x2+y2) : z=sqr(z2) ?

Post by D.J.Peters »

if x2+y2 <= 1 then z2 = 1-(x2+y2) : z = sqr(z2)

How to remove the sqr() to get z ?
I know there must be a simple 2D solution
we know:
radius and radius squared both 1 (Unit Circle)
the range of x: = -1 -> 0 -> +1
the range of y: = -1 -> 0 -> +1
x squared x2 = x*x
y squared y2 = y*y
z squared z2 = 1 - (x2 + y2)

Can you help or give me a small hint I'm stupid today ?
(my mother passed away few days ago so I need some distraction)

Thank you

Joshy

Here are the code with sqr()

Code: Select all

screenres 640,480,32
dim as integer w,h
screeninfo w,h
width w/8,h/16
dim as single r = 100, r2=r*r
dim as single cx = w/2.0, cy = h/2.0
dim as single sx=2.0/w
dim as single sy=2.0/h
for y as single = -1 to 1 step sy
  dim as single y2=y*y
  for x as single = -1 to 1 step sx
    dim as single x2=x*x
    if x2 + y2 <= 1 then
      dim as single z2 = 1 - (x2+y2)
      'dim as single IsOne = x2 + y2 + z2
      dim as single z = iif(z2,sqr(z2),0)      
      
      pset(cx + x*r,h-r + y*r),rgb(abs(x)*255,abs(y)*255,abs(z)*255)
      dim as single xr=(1+x)*r
      dim as single yr=(1+y)*r
      pset(xr    ,yr),rgb(abs(x)*255,0,0)
      pset(xr+r*2,yr),rgb(0,abs(y)*255,0)
      pset(xr+r*4,yr),rgb(0,0,abs(z)*255)      
    end if  
  next
next
draw string (r*0   ,r),"-1 X" : draw string (r*1-20,r)," 0 X" : draw string (r*2-32,r)     ,"+1 X"  
draw string (r*3-16,4),"+1 Y" : draw string (r*3-16,r)," 0 Y" : draw string (r*3-16,r*2-16),"-1 Y" 
draw string (r*5-16,4)," 0 Z" : draw string (r*5-16,r),"+1 Z" : draw string (r*5-16,r*2-16)," 0 Z"
sleep
Here are a faked try without sqr() z = (x2+y2)-1
Compare both results looks not bad but I need the correct solution.

Code: Select all

screenres 640,480,32
dim as integer w,h
screeninfo w,h
width w/8,h/16
dim as single r = 100, r2=r*r
dim as single cx = w/2.0, cy = h/2.0
dim as single sx=2.0/w
dim as single sy=2.0/h
for y as single = -1 to 1 step sy
  dim as single y2=y*y
  for x as single = -1 to 1 step sx
    dim as single x2=x*x
    if x2 + y2 <= 1 then
      dim as single z2 = 1 - (x2+y2)
      'dim as single IsOne = x2 + y2 + z2    
      dim as single z = (x2+y2)-1         
      
      pset(cx + x*r,h-r + y*r),rgb(abs(x)*255,abs(y)*255,abs(z)*255)
      dim as single xr=(1+x)*r
      dim as single yr=(1+y)*r
      pset(xr    ,yr),rgb(abs(x)*255,0,0)
      pset(xr+r*2,yr),rgb(0,abs(y)*255,0)
      pset(xr+r*4,yr),rgb(0,0,abs(z)*255)      
    end if  
  next
next
draw string (r*0   ,r),"-1 X" : draw string (r*1-20,r)," 0 X" : draw string (r*2-32,r)     ,"+1 X"  
draw string (r*3-16,4),"+1 Y" : draw string (r*3-16,r)," 0 Y" : draw string (r*3-16,r*2-16),"-1 Y" 
draw string (r*5-16,4)," 0 Z" : draw string (r*5-16,r),"+1 Z" : draw string (r*5-16,r*2-16)," 0 Z"
sleep
Last edited by D.J.Peters on Sep 25, 2020 23:07, edited 1 time in total.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: if x2+y2 <= 1 then z2=1-(x2+y2) : z=sqr(z2) ?

Post by D.J.Peters »

sqr()
Image
fake compare the blue z values not bad but not the right!
Image
Last edited by D.J.Peters on Sep 24, 2020 23:44, edited 1 time in total.
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: if x2+y2 <= 1 then z2=1-(x2+y2) : z=sqr(z2) ?

Post by badidea »

D.J.Peters wrote:How to remove the sqr() to get z ?
Not possible I think. You can draw a circle with radius 1, within this circle a point at distance x,y from center. Z would be the distance to the circumfence. But I see no way to get z without using sqr().
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: if x2+y2 <= 1 then z2=1-(x2+y2) : z=sqr(z2) ?

Post by D.J.Peters »

badidea wrote:But I see no way to get z without using sqr().
Stand on my shoulder and you will see much more as before :-)

I don't give up at this early stage :-)

Joshy
angros47
Posts: 2324
Joined: Jun 21, 2005 19:04

Re: if x2+y2 <= 1 then z2=1-(x2+y2) : z=sqr(z2) ?

Post by angros47 »

The reason you don't want to use sqr is for speed, right?

The first thing that comes to my mind: in your code, you use the value of z to calculate the blue level, and this level is an integer value between 0 and 255. Also, the value of z is supposed to vary between 0 and 1, it cannot go out of range, and that means even the value of z2 is supposed to vary between 0 and 1. So, why don't you use a precomputed lookup table?

Like:

Code: Select all

dim sqrtable(65536) as ubyte

for i as integer=0 to 65535
      dim t as single=cast(single, i)/65535
      sqrtable(i)=sqr(t)*255
next
After that, when you need to calculate the square root of z2 for the pset color argument, you just need to use something like:

Code: Select all

pset(xr+r*4,yr),rgb(0,0,sqrtable(abs(z)*65535) )  
I haven't tested it, not sure if it works, but it was just to give you an idea
Condolences, D.J. Peters
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: if x2+y2 <= 1 then z2=1-(x2+y2) : z=sqr(z2) ?

Post by D.J.Peters »

Code: Select all

screenres 640,480,32
dim as integer w,h
screeninfo w,h
width w/8,h/16
dim as single r = 100, r2=r*r
dim as single cx = w/2.0, cy = h/2.0
dim as single sx=2.0/w
dim as single sy=2.0/h
for y as single = -1 to 1 step sy
  dim as single y2=y*y
  for x as single = -1 to 1 step sx
    dim as single x2 = x*x
    dim as single d2 = x2 + y2
    if d2 <= 1 then
      dim as single z = 1 - (d2*d2)/1.41421356237
      
      pset(cx + x*r,h-r + y*r),rgb(abs(x)*255,abs(y)*255,z*255)
      dim as single xr=(1+x)*r
      dim as single yr=(1+y)*r
      pset(xr    ,yr),rgb(abs(x)*255,0,0)
      pset(xr+r*2,yr),rgb(0,abs(y)*255,0)
      pset(xr+r*4,yr),rgb(0,0,z*255)      
    end if  
  next
next
draw string (r*0   ,r),"-1 X" : draw string (r*1-20,r)," 0 X" : draw string (r*2-32,r)     ,"+1 X"  
draw string (r*3-16,4),"+1 Y" : draw string (r*3-16,r)," 0 Y" : draw string (r*3-16,r*2-16),"-1 Y" 
draw string (r*5-16,4)," 0 Z" : draw string (r*5-16,r),"+1 Z" : draw string (r*5-16,r*2-16)," 0 Z"
sleep
z = sqr(z2)
Image
z = 1- d2/1.414213562
Image
the winner: z = 1- (d2*d2)/1.414213562
Image
Last edited by D.J.Peters on Sep 25, 2020 1:47, edited 1 time in total.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: if x2+y2 <= 1 then z2=1-(x2+y2) : z=sqr(z2) ?

Post by D.J.Peters »

distance squared = x*x + y*y
z = (1*1) - (distance_squared *distance_squared ) / 1.414213562
=
z = 1 - (distance_squared *distance_squared ) / 1.414213562

a2 + b2 = c2
1*1 + 1*1 = 2
1.414213562 = sqr(2)

Is 99% accurat let me know if you find a better solution and thank you for your tips.

Joshy

EDIT:
I was thinking PNG compression is loseless but on the forum the normalized rgb 2D sphere has a ellipse inside but not if I run it on the monitor at home.
May be the result of a fast png decompresser from firefox I will compare it with an other browser.

Do you see the ellipse also ?

Joshy

EDIT 2:
I don't get it (REALLY) I captured a uncompressed BMP file with the 2D sphere from forum and monitor and you see only on the forum sphere the ellipse !
Than I uploaded the BMP file and the forum shows both spheres with this ellipse !
I opened a second browser believe it or not the eliipse was for the first second not visible but than it came back ?
Does the forum software read external linked images in any way ?

Here are the bmp file if I view it on desktop or in paint the top sphere does not have the ellipse but on the forum both have this eliipse now !
Image
Right click save as image and the ellipse on the top sphere not shown here !!!
Or ist a Windows 10 thing images in apps are smothed/blured/dicered or what ever like a font render engine ?
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: if x2+y2 <= 1 then z2=1-(x2+y2) : z=sqr(z2) ?

Post by fxm »

Try to replace:

Code: Select all

      dim as single z = iif(z2,sqr(z2),0)     
with:

Code: Select all

      ''  optimized for z2 in [0, 1] 
      dim as single z = z2
      if z > 0 then
        while abs(z * z - z2) > 0.001 * z2  '' error allowed
          z = (z + z2 / z) / 2
        wend
      end if
Last edited by fxm on Sep 25, 2020 21:36, edited 6 times in total.
UEZ
Posts: 988
Joined: May 05, 2017 19:59
Location: Germany

Re: if x2+y2 <= 1 then z2=1-(x2+y2) : z=sqr(z2) ?

Post by UEZ »

Here the result using inverse square root:

Code: Select all

'https://codegolf.stackexchange.com/questions/85555/the-fastest-square-root-calculator -> C (miles)
Function _Sqrt4(n As Single) As Single 'Fast inverse square root ->  https://en.wikipedia.org/wiki/Fast_inverse_square_root
	n = 1 / n
	Dim As ULong i
	Dim As Single x, y
	x = n * 0.5
	y = n
	i = *Cptr(ULong Ptr, @y)
	i = &h5F3759DF - (i Shr 1)
	y = *Cptr(Single Ptr, @i)
	Return y * (1.5 - (x * y * y))
End Function

screenres 640,480,32
dim as integer w,h
screeninfo w,h
width w/8,h/16
dim as single r = 100, r2=r*r
dim as single cx = w/2.0, cy = h/2.0
dim as single sx=2.0/w
dim as single sy=2.0/h
for y as single = -1 to 1 step sy
  dim as single y2=y*y
  for x as single = -1 to 1 step sx
    dim as single x2=x*x
    if x2 + y2 <= 1 then
      dim as single z2 = 1 - (x2+y2)
      'dim as single IsOne = x2 + y2 + z2
      dim as single z = iif(z2,_Sqrt4(z2),0)     
     
      pset(cx + x*r,h-r + y*r),rgb(abs(x)*255,abs(y)*255,abs(z)*255)
      dim as single xr=(1+x)*r
      dim as single yr=(1+y)*r
      pset(xr    ,yr),rgb(abs(x)*255,0,0)
      pset(xr+r*2,yr),rgb(0,abs(y)*255,0)
      pset(xr+r*4,yr),rgb(0,0,abs(z)*255)     
    end if 
  next
next
draw string (r*0   ,r),"-1 X" : draw string (r*1-20,r)," 0 X" : draw string (r*2-32,r)     ,"+1 X" 
draw string (r*3-16,4),"+1 Y" : draw string (r*3-16,r)," 0 Y" : draw string (r*3-16,r*2-16),"-1 Y"
draw string (r*5-16,4)," 0 Z" : draw string (r*5-16,r),"+1 Z" : draw string (r*5-16,r*2-16)," 0 Z"
sleep
Here my collections of Sqr functions:

Code: Select all

Function _ASM_Sqr1(n As Single) As Single
	Asm
		rsqrtss xmm0, [n]
		mulss   xmm0, [n]
		movss   [Function], xmm0		
	End Asm
End Function

Function _ASM_Sqr2(n As Single) As Single
	Asm
		fld dword Ptr [n]
		fsqrt
		fstp dword Ptr [Function]
	End Asm
End Function

'https://codegolf.stackexchange.com/questions/85555/the-fastest-square-root-calculator -> Python 3 (Leaky Nun)
Function _Sqrt1(n As Single) As Single
	Dim As Integer highest = 1, sqrt_highest = 1
	While highest < n
		highest Shl= 2
		sqrt_highest Shl= 1
	Wend
	n /= highest
	Dim As Single result
	result = (n / 4) + 1
    result = (result / 2) + (n / (result * 2))
    result = (result / 2) + (n / (result * 2))
	Return result * sqrt_highest
End Function

'http://www.codeproject.com/Articles/69941/Best-Square-Root-Method-Algorithm-Function-Precisi
Function _Sqrt2(x As Single) As Single
  Var y = *Cptr(Ulong Ptr, @x)
  Var s = y And &h80000000
  y And = &h7fffffff
  y +=127 Shl 23
  y Shr= 1
  y Or= s
  Return *Cptr(Single Ptr, @y)
End Function

'https://codegolf.stackexchange.com/questions/85555/the-fastest-square-root-calculator -> C (gcc) (jayprich)
Function _Sqrt3(n As Single) As Single
	Dim As Integer i = *Cptr(Ulong Ptr, @n)
	i = &h1FB90000 + (i Shr 1)
	Dim As Single y = *Cptr(Single Ptr, @i)
	Return (y + n / y) / 2
End Function

Function _Sqrt35(n As Single) As Single 'only 32-bit compatible
	Dim As Integer i = Cvi(n)
	i = &h1FB90000 + (i Shr 1)
	Dim As Single y = Cvs(i)
	Return (y + n / y) / 2
End Function

'https://codegolf.stackexchange.com/questions/85555/the-fastest-square-root-calculator -> C (miles)
Function _Sqrt4(n As Single) As Single 'Fast inverse square root ->  https://en.wikipedia.org/wiki/Fast_inverse_square_root
	n = 1 / n
	Dim As ULong i
	Dim As Single x, y
	x = n * 0.5
	y = n
	i = *Cptr(ULong Ptr, @y)
	i = &h5F3759DF - (i Shr 1)
	y = *Cptr(Single Ptr, @i)
	Return y * (1.5 - (x * y * y))
End Function

Dim As Single a = 4, b = 64, c = 10000, d = 2
? Sqr(a), _ASM_Sqr1(a), _ASM_Sqr2(a), _Sqrt1(a), _Sqrt2(a), _Sqrt3(a), _Sqrt4(a)
? Sqr(b), _ASM_Sqr1(b), _ASM_Sqr2(b), _Sqrt1(b), _Sqrt2(b), _Sqrt3(b), _Sqrt4(b)
? Sqr(c), _ASM_Sqr1(c), _ASM_Sqr2(c), _Sqrt1(c), _Sqrt2(c), _Sqrt3(c), _Sqrt4(c)
? Sqr(d), _ASM_Sqr1(d), _ASM_Sqr2(d), _Sqrt1(d), _Sqrt2(d), _Sqrt3(d), _Sqrt4(d)
Sleep
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: if x2+y2 <= 1 then z2=1-(x2+y2) : z=sqr(z2) ?

Post by counting_pine »

D.J.Peters wrote:EDIT:
I was thinking PNG compression is loseless but on the forum the normalized rgb 2D sphere has a ellipse inside but not if I run it on the monitor at home.
May be the result of a fast png decompresser from firefox I will compare it with an other browser.

Do you see the ellipse also ?

Joshy

EDIT 2:
I don't get it (REALLY) I captured a uncompressed BMP file with the 2D sphere from forum and monitor and you see only on the forum sphere the ellipse !
Than I uploaded the BMP file and the forum shows both spheres with this ellipse !
I opened a second browser believe it or not the eliipse was for the first second not visible but than it came back ?
Does the forum software read external linked images in any way ?
(Sorry to hear about your mother.. I will pray for you, I hope that's OK.)
Doing a quick diff in GIMP, I can see a horizontal green halo, and a vertical red ellipse as well.

Do you see a pear in https://superuser.com/q/579216/19792? If so, your browser is performing colour correction on images.

xxd tells me your PNG images (or at least the one that I checked) have sRGB and gAMA chunks, which provide colour space and gamma information for the image, which (in theory) allow it to be displayed the same on different screens, as long as they're properly calibrated.

(In practice, most of the time, I suspect people rarely care about getting their image looking exactly right on other screens, and assume that it looks roughly ok everywhere, relative to everything else on the screen.)

I'm currently on Linux without Wine, otherwise I'd use TweakPNG, which would tell me the exact gamma used. But I suspect it's probably close to the "natural" gamma. So the image is displayed mostly the same, but some colour values are wrong, perhaps due to imprecision or perhaps truncation in the colour correction values internally somewhere.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: if x2+y2 <= 1 then z2=1-(x2+y2) : z=sqr(z2) ?

Post by D.J.Peters »

Thank you all for your hints.

z = 1 - (d2*d2)/srq(2)

is not perfect but good as replacement for z = sqr(z2) and positive also (I removed abs() fom color term) RGB(abs(x)*255, abs(y)*255, z*255)

I was really thinking the information of z are aviable in the known numbers and I'm only blind to see it

But I was wrong :-)

Joshy
Post Reply