It is currently Fri Aug 14, 2026 12:02 am


All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 12 posts ] 
Author Message
 Post subject: Setting Decimal Places
PostPosted: Sat Aug 04, 2012 1:37 am  (#1) 
Offline
GimpChat Member
User avatar

Joined: Jul 14, 2012
Posts: 20
Hi,

I'm using this equation in a script-fu:

(set! slope (/ (- y2 y1)(- x2 x1)))
(set! angle (/ (* slope 180) 3.14))

and it's returning back values with 6 decimal places, I need to take it down to 2 decimal places.

I tried (angle (round angle)) but that doesn't work. It rounds the number but leaves the decimal places.

Any input is greatly appreciated!

Thank you


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: Setting Decimal Places
PostPosted: Sat Aug 04, 2012 2:03 am  (#2) 
Offline
Script Coder
User avatar

Joined: Jun 22, 2010
Posts: 1171
Location: Here and there
I'm sure I've seen RobA answer a very similar question to this, but I don't know on which Forum he did so...

My solution would be to multiply by 100, turn the value into an integer, then divide by 100.0

(set! angle (/ (round (* 100 angle)) 100.0))

Kevin


Top
 Post subject: Re: Setting Decimal Places
PostPosted: Sat Aug 04, 2012 2:44 am  (#3) 
Offline
Script Coder
User avatar

Joined: Apr 23, 2010
Posts: 1553
Location: not from Guildford after all
I agree with paynekj, however, I would advise against using the 'round' function since its behavior is based upon statistical "tie-breaking" style; i.e., "Round Half To Even". This style's behavior is not generally conducive to graphics programming since whether a *.5 value rounds up or down is dependent upon what the integer part is.

For example,

(round 2.5) ==> 2 ; rounds down

(round 3.5) == 4 ; rounds up


I find it is typically preferable to always Round Half Up (at least this is consistent).

(set! angle (/ (truncate ((if (< angle 0) - +) (* 100 angle) 0.5)) 100.0))

_________________
Any sufficiently primitive technology is indistinguishable from a rock.


Top
 Post subject: Re: Setting Decimal Places
PostPosted: Sat Aug 04, 2012 4:03 am  (#4) 
Offline
GimpChat Member
User avatar

Joined: Jul 14, 2012
Posts: 20
Thanks for responding Kevin & saulgoode, but oddly it doesn't seem to be working. I'm usually very tired when I make these posts, because I truly make the Google rounds looking for answers, checking scripts, etc. I try to make this my last resort. I'm going to post the full script and maybe that will help.

Basically, I'm trying to draw random lines but at the same time adjust the brush angle so that all the lines look thin, as if they were drawn on an exact horizontal line set at 0 degrees. I chose the 2. Block 03 brush because it is easy to see when the degrees are off. When the degrees change perfectly, it's a thin line. But when the degrees are off it's a thick line.

But, as I said before it crashes. The error I get is:

Error: ( : 1) Procedure execution of gimp-context-set-brush-angle failed on invalid input arguments: Procedure 'gimp-context-set-brush-angle' has been called with value '270.610000' for argument 'angle' (#1, type gdouble). This value is out of range.

The value is always different.

Additionally, It'll draw a few lines. They start off thin, then quickly become thick, then the script crashes with that error. The results are different every time I run it, I guess that's the random portion at work.

(define (script-fu-angle-test image drawable lines)

  (let*

    (
     
     (image-width (car (gimp-image-width image)))
     (image-height (car (gimp-image-height image)))
     (size-x image-width)
     (size-y image-height)
     (vector-array (make-vector 4 'double))
     (count 0)
     (slope)
     (angle)
     (x1)
     (x2)
     (y1)
     (y2)

     )
;start undo
      (gimp-image-undo-group-start image)
           
          ;Set drawable color, brush and brush size
          (gimp-context-set-foreground '(0 0 0))
          (gimp-context-set-brush "2. Block 03")
          (gimp-context-set-brush-size 20)
     
     ;Starting drawing random lines 
    (while (< count lines)
           (set! count (+ count 1))
         
           ;Setting xy coordinates
           (vector-set! vector-array 0 (random size-x))
           (vector-set! vector-array 1 (random size-y))
           (vector-set! vector-array 2 (random size-x))
           (vector-set! vector-array 3 (random size-y))

           ;Recalling xy coordinates for slope equation
           (set! x1 (aref vector-array 0))
           (set! y1 (aref vector-array 1))
           (set! x2 (aref vector-array 2))
           (set! y2 (aref vector-array 3))
           
           ;Calculating angle
           (set! slope (/ (- y2 y1)(- x2 x1)))
           (set! angle (/ (* slope 180) *pi*))
           
           ;Problem somewhat corrected here. Discussed in prior replies in this thread.
           
           ;Setting angle & creating drawable
           (gimp-context-set-brush-angle angle)
           (gimp-paintbrush-default drawable 4 vector-array)
           (gimp-displays-flush)
             
     )

;Finish Undo & Refresh         
       (gimp-image-undo-group-end image)
       (gimp-displays-flush)
       
   )

)

(script-fu-register "script-fu-angle-test"
            "<Image>/Filters/Angle Test"
            "Angle Test"
            "PD"   
            "PD"   
            "2012"
            ""           
            SF-IMAGE "image" 0
            SF-DRAWABLE "drawable" 0
            SF-ADJUSTMENT "Number of random lines created" '(24 2 100 2 10 0 0)

)


Thank you for your help!


Last edited by Phoenix999 on Sat Aug 04, 2012 3:48 pm, edited 1 time in total.

Top
 Post subject: .
PostPosted: Sat Aug 04, 2012 8:43 am  (#5) 
Offline
GimpChat Member

Joined: Apr 19, 2012
Posts: 80
Location: Australia
.


Last edited by b10h4z4rd on Thu Sep 06, 2012 5:26 pm, edited 1 time in total.

Top
 Post subject: Re: Setting Decimal Places
PostPosted: Sat Aug 04, 2012 3:51 pm  (#6) 
Offline
GimpChat Member
User avatar

Joined: Jul 14, 2012
Posts: 20
Quote:
That is because the only valid values for the parameter 'angle' in 'gimp-context-set-brush-angle' are:- angle in degrees (-180 <= angle <= 180)


Right. Thanks for clarifying something that I clearly should have been taking into consideration.

I should add I am new to gimp and coding (let alone Script-fu). I believe I've made positive progress since I've started and I am definitely learning from my mishaps and I am always thankful when people are willing to give me a helping hand. I am just a humble grasshopper trying to make sense of a language I've never written. And as I've said before, I take great efforts before asking for help here. Thou in this case I admit the Procedure Browser was just a few clicks away from identifying my error.

I've added this code below the angle equation to see if it corrects the problem:

(if (> angle 180)
           (set! angle (- angle 180))
       )       
       
       (if (< angle -180)
           (set! angle (+ angle 180))
       )


It somewhat does. It allows the script to run most of the time, however halfway through it starts drawing thick lines and on occasion it gives me an error similar to the one mentioned before.

Any thoughts are welcome.


Top
 Post subject: .
PostPosted: Sat Aug 04, 2012 5:48 pm  (#7) 
Offline
GimpChat Member

Joined: Apr 19, 2012
Posts: 80
Location: Australia
.


Last edited by b10h4z4rd on Thu Sep 06, 2012 5:26 pm, edited 1 time in total.

Top
 Post subject: Re: Setting Decimal Places
PostPosted: Sat Aug 04, 2012 6:34 pm  (#8) 
Offline
Script Coder
User avatar

Joined: Apr 23, 2010
Posts: 1553
Location: not from Guildford after all
Phoenix999 wrote:
I've added this code below the angle equation to see if it corrects the problem:

(if (> angle 180)
           (set! angle (- angle 180))
       )       
       
       (if (< angle -180)
           (set! angle (+ angle 180))
       )


It somewhat does. It allows the script to run most of the time, however halfway through it starts drawing thick lines and on occasion it gives me an error similar to the one mentioned before.

Any thoughts are welcome.

Change the two 'if's to 'while's

_________________
Any sufficiently primitive technology is indistinguishable from a rock.


Top
 Post subject: Re: Setting Decimal Places
PostPosted: Sat Aug 04, 2012 7:31 pm  (#9) 
Offline
GimpChat Member
User avatar

Joined: Jul 14, 2012
Posts: 20
To b10h4z4rd

Thank you. I too figured out that my angle equation is definitely faulty. I'll need to redo that portion in someway or another.

I'm definitely going to try to absorb (and research more) your input on the error console. Plus the truncate addition. Both are extremely helpful. Thanks again!

To saulgoode

the "while"'s definitely stopped the script from crashing and that is a relief. Thank you very much!


Top
 Post subject: .
PostPosted: Sat Aug 04, 2012 7:43 pm  (#10) 
Offline
GimpChat Member

Joined: Apr 19, 2012
Posts: 80
Location: Australia
.


Last edited by b10h4z4rd on Thu Sep 06, 2012 5:27 pm, edited 1 time in total.

Top
 Post subject: Re: Setting Decimal Places
PostPosted: Mon Aug 13, 2012 2:47 pm  (#11) 
Offline
GimpChat Member
User avatar

Joined: Jul 14, 2012
Posts: 20
After a little digging, I found the proper formula to calculate the angle using just the slope.

(180 * Arctan(slope) )/ Pi

or in Script-fu

(/ (* 180 (atan slope)) (* 4 (atan 1.0))))

or more specifically

(set! slope (/ (- y2 y1)(- x2 x1)))
(set! angle (/ (* 180 (atan slope)) (* 4 (atan 1.0))))

Hope that is useful for someone.


Top
 Post subject: Re: Setting Decimal Places
PostPosted: Mon Aug 13, 2012 3:28 pm  (#12) 
Offline
Script Coder
User avatar

Joined: Apr 23, 2010
Posts: 1553
Location: not from Guildford after all
A common way of computing the angle given by a particular slope is using the atan2 function. It requires, not the slope, but its delta-y and delta-x.

It has the advantage of always returning a value between -180 degrees (-π radians) and 180 degrees (
π radians) and properly distinguishing which quadrant of the Cartesian grid the slope occurs in.

(define (atan2 dy dx)
    (if (> dx 0)
      (atan (/ dy dx))
      (if (< dx 0)
        (if (< dy 0)
          (- (atan (/ dy dx)) *pi*)
          (+ (atan (/ dy dx)) *pi*) )
        (cond ;; dx is zero
          ((> dy 0) (/ *pi* 2))
          ((< dy 0) (- (/ *pi* 2)))
          (else ;; dx==dy==0 is typically undefined but we return 0 instead
            0 )))))

_________________
Any sufficiently primitive technology is indistinguishable from a rock.


Top
Post new topic Reply to topic  [ 12 posts ] 

All times are UTC - 5 hours [ DST ]



* Login  



Powered by phpBB3 © phpBB Group