POV-Ray : Newsgroups : povray.binaries.images : Rotation matrices Server Time
20 Apr 2024 02:39:02 EDT (-0400)
  Rotation matrices (Message 11 to 15 of 15)  
<<< Previous 10 Messages Goto Initial 10 Messages
From: Tor Olav Kristensen
Subject: Re: Rotation matrices
Date: 7 Apr 2021 00:50:00
Message: <web.606d38e0565eea128e52cc8789db30a9@news.povray.org>
"Bald Eagle" <cre### [at] netscapenet> wrote:
> "Tor Olav Kristensen" <tor### [at] TOBEREMOVEDgmailcom> wrote:
> ...  I would also like to figure out if there's a nice
> degrees-to-fractions-of-pi algorithm out there, and I could implement that.
> I'll probably just hard-code an array of text objects.

You can divide the angle by 180 and convert the resulting decimal number to a
fraction. Then you can use David Eppstein's "find rational approximation to
given real number" code as suggested in the first answer here:

https://stackoverflow.com/questions/95727/how-to-convert-floats-to-human-readable-fractions


>...
> Do you watch 3blue1brown's channel?   It's - peaceful, and elegant, and
> mesmerizing.  It's one of the best things to happen to math in a long while.
> :)

Yes, I've seen some of his videos. Very good =)
I'm looking forward to the rest of them.

--
Tor Olav
http://subcube.com
https://github.com/t-o-k


Post a reply to this message

From: Bald Eagle
Subject: Re: Rotation matrices
Date: 2 Mar 2023 07:20:00
Message: <web.6400937d565eea121f9dae3025979125@news.povray.org>
"Tor Olav Kristensen" <tor### [at] TOBEREMOVEDgmailcom> wrote:
> "Bald Eagle" <cre### [at] netscapenet> wrote:
> > "Tor Olav Kristensen" <tor### [at] TOBEREMOVEDgmailcom> wrote:
> > ...  I would also like to figure out if there's a nice
> > degrees-to-fractions-of-pi algorithm out there, and I could implement that.
> > I'll probably just hard-code an array of text objects.
>
> You can divide the angle by 180 and convert the resulting decimal number to a
> fraction. Then you can use David Eppstein's "find rational approximation to
> given real number" code as suggested in the first answer here:
>
>
https://stackoverflow.com/questions/95727/how-to-convert-floats-to-human-readable-fractions


I tried at least 2 different ways to go about this, but I didn't have any
success and fell into some kind of trap in the recursion programming that
short-circuits things.   It was a mess that I spent too many hours trying to
fix.

I have more application cases that I'd like to apply this sort of thing to, if
you give me a nudge in the proper direction.

Thanks,

- BW


Post a reply to this message


Attachments:
Download 'rationalapproximation.pov.txt' (7 KB)

From: Tor Olav Kristensen
Subject: Re: Rotation matrices
Date: 2 Mar 2023 13:35:00
Message: <web.6400ebee565eea12e9c9e5da89db30a9@news.povray.org>
"Bald Eagle" <cre### [at] netscapenet> wrote:
> "Tor Olav Kristensen" <tor### [at] TOBEREMOVEDgmailcom> wrote:
> > "Bald Eagle" <cre### [at] netscapenet> wrote:
> > > "Tor Olav Kristensen" <tor### [at] TOBEREMOVEDgmailcom> wrote:
> > > ...  I would also like to figure out if there's a nice
> > > degrees-to-fractions-of-pi algorithm out there, and I could implement that.
> > > I'll probably just hard-code an array of text objects.
> >
> > You can divide the angle by 180 and convert the resulting decimal number to a
> > fraction. Then you can use David Eppstein's "find rational approximation to
> > given real number" code as suggested in the first answer here:
> >
> >
https://stackoverflow.com/questions/95727/how-to-convert-floats-to-human-readable-fractions
>
>
> I tried at least 2 different ways to go about this, but I didn't have any
> success and fell into some kind of trap in the recursion programming that
> short-circuits things.   It was a mess that I spent too many hours trying to
> fix.
>
> I have more application cases that I'd like to apply this sort of thing to, if
> you give me a nudge in the proper direction.

Hi

As I know that you are very fond of Python, I'll nudge you to have a
look this code (which SEEMS to work ok):


# real_num: real number to approximate
# max_denom: maximum denominator allowed

def real_to_fraction(real_num, max_denom):

    max_int = 2**31 - 1  # LONG_MAX == 0x7FFFFFFF in C
    x = real_num
    max_denom = int(max_denom)
    m00 = int(1)
    m01 = int(0)
    m10 = int(0)
    m11 = int(1)

    # loop finding terms until denominator gets too big
    ai = int(x)
    while (m10*ai + m11 <= max_denom):
        tmp = m00*ai + m01
        m01 = int(m00)
        m00 = int(tmp)
        tmp = m10*ai + m11
        m11 = int(m10)
        m10 = int(tmp)
        if (x == ai):
            break  # AF: division by zero
        x = 1/(x - ai)
        if (abs(x) > max_int):
            break  # AF: representation failure
        ai = int(x)

    # now remaining x is between 0 and 1/ai
    # approximate as either 0 or 1/m where
    # m is max that will fit in max_denom

    # first try zero
    error = real_num - m00/m10
    print(f'{m00}/{m10}, error = {error}\n')

    # then try other possibility
    ai = int((max_denom - m11)/m10)
    m00 = int(m00*ai + m01)
    m10 = int(m10*ai + m11)
    error = real_num - m00/m10
    print(f'{m00}/{m10}, error = {error}\n')


Test run:

    real_to_fraction(3.875, 100)

    31/8, error = 0.0

    376/97, error = -0.0012886597938144284


The type long in C is the same as the type signed long int.
So I put  int() around every expression going into variables
of type long.

As you know, I don't like break statements in loops, so this
remains to be fixed.

In Python max_int should probably be chosen to be maxsize or
maxsize - 1 (after doing "from sys import maxsize").


--
Tor Olav
http://subcube.com
https://github.com/t-o-k


Post a reply to this message

From: Tor Olav Kristensen
Subject: Re: Rotation matrices
Date: 3 Mar 2023 09:45:00
Message: <web.6402079b565eea12d105cea189db30a9@news.povray.org>
"Tor Olav Kristensen" <tor### [at] TOBEREMOVEDgmailcom> wrote:
>...
>         tmp = m00*ai + m01
>         m01 = int(m00)
>         m00 = int(tmp)
>         tmp = m10*ai + m11
>         m11 = int(m10)
>         m10 = int(tmp)
>...

Sorry, that was not very Pythonic. I try again:

        m00, m01 = int(m00*ai + m01), int(m00)
        m10, m11 = int(m10*ai + m11), int(m10)

--
Tor Olav
http://subcube.com
https://github.com/t-o-k


Post a reply to this message

From: Tor Olav Kristensen
Subject: Re: Rotation matrices
Date: 3 Mar 2023 13:25:00
Message: <web.64023b43565eea12d105cea189db30a9@news.povray.org>
"Bald Eagle" <cre### [at] netscapenet> wrote:
> "Tor Olav Kristensen" <tor### [at] TOBEREMOVEDgmailcom> wrote:
> > "Bald Eagle" <cre### [at] netscapenet> wrote:
> > > "Tor Olav Kristensen" <tor### [at] TOBEREMOVEDgmailcom> wrote:
> > > ...  I would also like to figure out if there's a nice
> > > degrees-to-fractions-of-pi algorithm out there, and I could implement that.
> > > I'll probably just hard-code an array of text objects.
> >
> > You can divide the angle by 180 and convert the resulting decimal number to a
> > fraction. Then you can use David Eppstein's "find rational approximation to
> > given real number" code as suggested in the first answer here:
> >
> >
https://stackoverflow.com/questions/95727/how-to-convert-floats-to-human-readable-fractions
>
>
> I tried at least 2 different ways to go about this, but I didn't have any
> success and fell into some kind of trap in the recursion programming that
> short-circuits things.   It was a mess that I spent too many hours trying to
> fix.
>
> I have more application cases that I'd like to apply this sort of thing to, if
> you give me a nudge in the proper direction.

I just had a look at you code and found that perhaps you are
misunderstanding what the value of this C expression is:

(ai = (long)x)

This is an assignment, where the value in X is converted to an
unsigned long int, then converted to the type of ai (which is
also an unsigned long int) and then stored in ai. The value of
the whole parenthesis expression is now equal to ai and can be
used in further calculations.

See this page:
https://stackoverflow.com/questions/9514569/what-does-an-assignment-return

In POV-Ray's SDL this expression:

(ai = x)

- is a comparison that is true when ai equals x and false otherwise.
So the value of the parenthesis expression is either 0 (for false)
or 1 (for true).

See "Relational expressions" (and the text right before) here:
https://wiki.povray.org/content/Reference:Numeric_Expressions

Also, you should call your macros with the second argument
(for the d parameter) greater than or equal to the greatest
denominator that you will accept. I.e. If you will accept
denominators up to 100, then pass 100 (not something small as
0.000001).

--
Tor Olav
http://subcube.com
https://github.com/t-o-k


Post a reply to this message

<<< Previous 10 Messages Goto Initial 10 Messages

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.