It is currently Mon Apr 15, 2024 7:59 am


All times are UTC - 5 hours [ DST ]


Switch to mobile style

Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: What's the math behind this?
PostPosted: Mon Apr 18, 2016 10:49 pm  (#1) 
Offline
Script Coder
User avatar

Joined: May 07, 2014
Posts: 3975
Location: Canada
look at this picture
Image

I am trying to write a script that asks user for N sections they want to have and then divide
a square (or rectangle) image into that many NxN squares (or rectangles).

I thought if i loop from 1 to N for Y and then inside that loop i loop from 1 to N for X
selecting the square or rectangle..
using math
I can perspective it into the image shown above...
but i am stuck at the math...
so basically I got cos(180 degrees) down to cos(0 degrees) ..i put degrees here but i actually convert it to radians before calling cos...
But I am still stumpled at the math to get the mapping of these squares/rectangles into the shape shown above.
i am assuming if i have the math down, a rectangle image will turn to an oval shape and a square will turn into a circle...
anyone can offer any help on the math behind this?

_________________
TinT


Share on Facebook Share on Twitter Share on Orkut Share on Digg Share on MySpace Share on Delicious Share on Technorati
Top
 Post subject: Re: What's the math behind this?
PostPosted: Tue Apr 19, 2016 1:20 pm  (#2) 
Offline
Global Moderator
User avatar

Joined: Nov 16, 2011
Posts: 5128
Location: Metro Vancouver, BC
Wolfram MathWorld is worth a look.

_________________
Image
Gimp 2.8.18, Linux, median user
Gimp Chat Tutorials Index
Spirit Bear (Kermode)


Top
 Post subject: Re: What's the math behind this?
PostPosted: Tue Apr 19, 2016 3:19 pm  (#3) 
Offline
Script Coder
User avatar

Joined: Oct 25, 2010
Posts: 4734
trandoductin wrote:
look at this picture
[ Image ]

I am trying to write a script that asks user for N sections they want to have and then divide
a square (or rectangle) image into that many NxN squares (or rectangles).

I thought if i loop from 1 to N for Y and then inside that loop i loop from 1 to N for X
selecting the square or rectangle..
using math
I can perspective it into the image shown above...
but i am stuck at the math...
so basically I got cos(180 degrees) down to cos(0 degrees) ..i put degrees here but i actually convert it to radians before calling cos...
But I am still stumpled at the math to get the mapping of these squares/rectangles into the shape shown above.
i am assuming if i have the math down, a rectangle image will turn to an oval shape and a square will turn into a circle...
anyone can offer any help on the math behind this?



Assuming center at 0,0:
a=atan2(y,x) # not the same as atan(y/x)!
X=x*cos(a)
Y=y*sin(a)

_________________
Image


Top
 Post subject: Re: What's the math behind this?
PostPosted: Tue Apr 19, 2016 3:24 pm  (#4) 
Offline
Script Coder
User avatar

Joined: Jun 22, 2010
Posts: 1171
Location: Here and there
I can create pretty much the same thing using the plug-in-lens-distortion run a few times on a grid:

Attachment:
Untitled.png
Untitled.png [ 168.07 KiB | Viewed 2118 times ]


(define (script-fu-kp_lens aimg adraw)
  (let* (
          (space 32)
          (thick 2)
        )
   (gimp-ellipse-select aimg 0 0 (car (gimp-image-width aimg)) (car (gimp-image-height aimg)) CHANNEL-OP-REPLACE TRUE FALSE 0.0)
   (plug-in-grid RUN-NONINTERACTIVE aimg adraw thick space 0 '(0 0 0) 255 thick space 0 '(0 0 0) 255 0 2 6 '(0 0 0) 255)
   (plug-in-lens-distortion RUN-NONINTERACTIVE aimg adraw 0.0 0.0 100.0 100.0 0.0 0.0)
   (plug-in-lens-distortion RUN-NONINTERACTIVE aimg adraw 0.0 0.0 100.0 100.0 0.0 0.0)
   (plug-in-lens-distortion RUN-NONINTERACTIVE aimg adraw 0.0 0.0 100.0 100.0 0.0 0.0)
   (plug-in-lens-distortion RUN-NONINTERACTIVE aimg adraw 0.0 0.0 100.0 100.0 0.0 0.0)
   (plug-in-lens-distortion RUN-NONINTERACTIVE aimg adraw 0.0 0.0 100.0 100.0 0.0 0.0)
   (plug-in-lens-distortion RUN-NONINTERACTIVE aimg adraw 0.0 0.0 100.0 100.0 0.0 0.0)

; flush the image
   (gimp-displays-flush)
  )
)


Kevin


Top
 Post subject: Re: What's the math behind this?
PostPosted: Tue Apr 19, 2016 3:57 pm  (#5) 
Offline
Script Coder
User avatar

Joined: May 07, 2014
Posts: 3975
Location: Canada
ofnuts wrote:
trandoductin wrote:
look at this picture
[ Image ]

I am trying to write a script that asks user for N sections they want to have and then divide
a square (or rectangle) image into that many NxN squares (or rectangles).

I thought if i loop from 1 to N for Y and then inside that loop i loop from 1 to N for X
selecting the square or rectangle..
using math
I can perspective it into the image shown above...
but i am stuck at the math...
so basically I got cos(180 degrees) down to cos(0 degrees) ..i put degrees here but i actually convert it to radians before calling cos...
But I am still stumpled at the math to get the mapping of these squares/rectangles into the shape shown above.
i am assuming if i have the math down, a rectangle image will turn to an oval shape and a square will turn into a circle...
anyone can offer any help on the math behind this?



Assuming center at 0,0:
a=atan2(y,x) # not the same as atan(y/x)!
X=x*cos(a)
Y=y*sin(a)

I tried this formula and found an atan2 defined somewhere else on the web...and got this
Image
; script: SphereFold.scm
; author: Tin Tran
; date: April 18, 2016
; experimental
(define (atan2 y x)
    (if (> x 0)
      (atan (/ y x))
      (if (< x 0)
        (if (< y 0)
          (- (atan (/ y x)) *pi*)
          (+ (atan (/ y x)) *pi*) )
        (cond ;; x is zero
          ((> y 0) (/ *pi* 2))
          ((< y 0) (- (/ *pi* 2)))
          (else ;; x==y==0 is typically undefined but we return 0 instead
            0 ) ) ) ) )
(define (script-fu-sphere-fold image layer
            sections         
         )
   
      (let*
         (
         (copied-layer 0)
         (width (car (gimp-image-width image)))
         (height (car (gimp-image-height image)))
         (y-section 0)
         (x-section 0)
         (section-angle (/ 180 sections))
         (section-width (/ width sections))
         (section-height (/ height sections))
         (x-radius (/ width 2))
         (y-radius (/ height 2))
         (x-angle-start 0)
         (y-angle-start 0)
         (x-angle-end 0)
         (y-angle-end 0)
         (x-select-start 0)
         (y-select-start 0)
         (x-select-end 0)
         (y-select-end 0)
         (transformed-item 0)
         (Opposite 0)
         (Adjacent 0)
         (top-left-angle 0)
         (top-right-angle 0)
         (bottom-left-angle 0)
         (bottom-right-angle 0)
         )
         ;(gimp-image-undo-disable image); DN = NO UNDO
         
         (gimp-image-undo-group-start image)                   ;undo-group in one step
         ;(gimp-image-resize image (* width 2) (* height 2) (/ width 2) (/ height 2))
         ;(gimp-layer-resize-to-image-size layer)
         (set! y-angle-start 180)
         (set! y-select-start 0)
         (while (< y-section sections)
            
            (set! y-angle-end (- y-angle-start section-angle))
            (set! y-select-end (+ y-select-start section-height))
            
             (set! x-section 0)
            (set! x-angle-start 180)
            (set! x-select-start 0)
            (while (< x-section sections)
               (set! x-angle-end (- x-angle-start section-angle))
               (set! x-select-end (+ x-select-start section-width))
               
               (gimp-image-select-rectangle image CHANNEL-OP-REPLACE x-select-start y-select-start section-width section-height)
               
               ; top-left angle
               (set! Adjacent (- x-select-start x-radius))
               (set! Opposite (- y-select-start y-radius))
               (set! top-left-angle (atan2 Opposite Adjacent))
               ; top-right angle
               (set! Adjacent (- x-select-end x-radius))
               (set! Opposite (- y-select-start y-radius))
               (set! top-right-angle (atan2 Opposite Adjacent))
               
               ; bottom-left-angle
               (set! Adjacent (- x-select-start x-radius))
               (set! Opposite (- y-select-end y-radius))
               (set! bottom-left-angle (atan2 Opposite Adjacent))
               ; bottom-right-angle
               (set! Adjacent (- x-select-end x-radius))
               (set! Opposite (- y-select-end y-radius))
               (set! bottom-right-angle (atan2 Opposite Adjacent))
               
               (set! transformed-item (car (gimp-item-transform-perspective layer
                                               (- x-radius (*(cos top-left-angle) (abs (- x-radius x-select-start)) ))
                                       (- y-radius (*(sin top-left-angle) (abs (- y-radius y-select-start)) ))
                                       
                                       (- x-radius (*(cos top-right-angle) (abs (- x-radius x-select-end)) ))
                                       (- y-radius (*(sin top-right-angle) (abs (- y-radius y-select-start)) ))
                                       
                                       (- x-radius (*(cos bottom-left-angle) (abs (- x-radius x-select-start)) ))
                                       (- y-radius (*(sin bottom-left-angle) (abs (- y-radius y-select-end)) ))
                                       
                                       (- x-radius (*(cos bottom-right-angle) (abs (- x-radius x-select-end)) ))
                                       (- y-radius (*(sin bottom-right-angle) (abs (- y-radius y-select-end)) ))
                                       
                                    )
                                 )
               )
               
               (gimp-floating-sel-to-layer transformed-item)
               
               (set! x-angle-start (- x-angle-start section-angle))
               (set! x-select-start (+ x-select-start section-width))
               (set! x-section (+ x-section 1))
            )
            (set! y-angle-start (- y-angle-start section-angle))
            (set! y-select-start (+ y-select-start section-height))
            (set! y-section (+ y-section 1))
         )
         
         ;(gimp-image-undo-enable image) ;DN = NO UNDO
         (gimp-image-undo-group-end image)                     ;undo group in one step
         (gimp-displays-flush)
       )
   
   
   
) ;end of define
(script-fu-register
  "script-fu-sphere-fold"         ;function name
  "<Image>/Script-Fu/Create New/Sphere Fold ..."    ;menu register
  "Creates Map Fold effect"       ;description
  "Tin Tran"                          ;author name
  "copyright info and description"         ;copyright info or description
  "2016"                          ;date
  "RGB*, GRAY*"                        ;mode
  SF-IMAGE      "Image" 0                   
  SF-DRAWABLE   "Layer" 0
  SF-ADJUSTMENT "Fold sections" '(3 2 30 1 5 0 0)
)



any idea why it looks like it's the inverted sphere and divided into 4 quarters..instead of what i want?

I thought maybe there's something wrong with my scheme script and thought i'd try a test on codeskulptor with the same formula and I am not getting a quarter of a circle like i was hoping codeskulptor page that uses formula

_________________
TinT


Top
 Post subject: Re: What's the math behind this?
PostPosted: Tue Apr 19, 2016 5:49 pm  (#6) 
Offline
Script Coder
User avatar

Joined: Oct 25, 2010
Posts: 4734
trandoductin wrote:

any idea why it looks like it's the inverted sphere and divided into 4 quarters..instead of what i want?

I thought maybe there's something wrong with my scheme script and thought i'd try a test on codeskulptor with the same formula and I am not getting a quarter of a circle like i was hoping codeskulptor page that uses formula


Absolutely no idea. To figure that out I would add print statements (or gimp messages) at a few places in the code to see what the values are and how often some code blocks are executed.

_________________
Image


Top
 Post subject: Re: What's the math behind this?
PostPosted: Sat Jun 04, 2016 2:24 pm  (#7) 
Offline
GimpChat Member
User avatar

Joined: Jul 04, 2010
Posts: 2253
Location: Retired Moderator Slowly Returning to the Living.
I would look at Filters>Map>Map Object
Select Map to Sphere.

Check out viewtopic.php?f=10&t=430 or
viewtopic.php?f=10&t=429 for some ideas.
These are two of my tutorials here on GC that I think will help you.

_________________
Artists aren't crazy! We're eccentric! ~G.M. Ross

Image

My Sigs = My Photos
Check out my work at http://www.flickr.com/photos/photomastergreg.


Top
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


   Similar Topics   Replies 
No new posts math map extras

2

No new posts Problem Solved Math Map 32 bit vs GMIC 64 bit

1

No new posts Attachment(s) math tools like union and intersect?

7



* Login  



Powered by phpBB3 © phpBB Group