Skip navigation
maxrus2012
Currently Being Moderated

script to calculate the hypotenuse of a page and place a picture box

Apr 3, 2012 11:35 PM

Hi,

I'm wondering if someone can help me with a script.

It needs to get the size of an open document, calculate the hypotenuse (diagonal), divide this by 10 and place a square picture box on the page at that size in the top left corner.

I found this javascript to calculate the hypotenuse but don't know how to use it or integrate it.

 

function hypotenuse(a, b) {

   function square(x) { return x*x; }

  

   return Math.sqrt(square(a) + square(b));

}

function secondFunction(){

   var result;

   result = hypotenuse(1,2);

   alert( result );

}

 

Yes I'm a total newbie and would appreciate anyones help.

Thanks,

maxrus2012

 
Replies
  • Currently Being Moderated
    Apr 6, 2012 7:26 PM   in reply to maxrus2012

    Hi maxrus,

     

    It sounds so simple that no one has found it necessary to answer you! Indeed, we can easily retrieve the height and the width of a page, then calculate the diagonal, then create a square based on that length.

     

    Anyway, let's try to make such routine work in any context, supporting any custom measurement units, rulers settings, rotated spreads and/or even scaled/skewed pages! To do so we need to avoid usual methods based on "geometric bounds". Interesting challenge!

     

    Here is my attempt:

     

    // Create a square based on the active page's diagonal length (10%)
    // ===========================
     
    function measureDiagonal(/*Page*/page)
    // -------------------------------------
    // Ret. the page's diagonal in pts (relative to the page CS)
    {
        var CS_INNER = CoordinateSpaces.innerCoordinates;
        
        var wh = page.resolve(AnchorPoint.bottomRightAnchor, CS_INNER)[0],
            w = wh[0],
            h = wh[1];
     
        return Math.sqrt(w*w + h*h); // Pythagorean theorem
    }
     
    function createTopLeftCornerRectangle(/*Page*/page, /*num[2]*/wh)
    // -------------------------------------
    // wh: width and height of the rectangle in pts (relative to the page CS)
    {
        // Some const shortcuts
        // ---
        var    CS_SPREAD = CoordinateSpaces.spreadCoordinates,
            CS_INNER = CoordinateSpaces.innerCoordinates,
            RM_REPLACE = ResizeMethods.replacingCurrentDimensionsWith,
            AP_TOP_LEFT = AnchorPoint.topLeftAnchor;
     
        var spread = page.parent,
            // Create a rectangle (in the spread CS--the page CS is not relevant yet)
            // ---
            rec = spread.rectangles.add({fillColor:'Black'}),
            // Page transformation values (relative to the spread)
            // ---
            pageMxValues = page.transformValuesOf(CS_SPREAD)[0].matrixValues;
            
     
        // Normalize the rectangle in the spread
        // (the size does not matter here)
        // ---
        rec.reframe(CS_SPREAD, [[0,0],[10,10]]);
        
        // Apply the page transfo to the rectangle
        // so that its inner space fits the page space
        // ---
        rec.transform(CS_SPREAD, [[0,0], CS_SPREAD], pageMxValues);
        
        // Finally, resize the rec
        // ---
        rec.resize(CS_INNER, AP_TOP_LEFT, RM_REPLACE, wh.concat(CS_INNER));
        
        return rec;
    }
     
    var    FACTOR = .1, // 10%
        win = app.layoutWindows.length && app.activeWindow,
        page = win && (win instanceof LayoutWindow) && win.activePage,
        size = page && page.isValid && FACTOR*measureDiagonal(page);
        
     
    size && createTopLeftCornerRectangle(page, [size,size]);
    

     

    Not sure this is exactly what you were looking after.

    Anyway...

     

    @+

    Marc

     
    |
    Mark as:
  • John Hawkinson
    5,572 posts
    Jun 25, 2009
    Currently Being Moderated
    Apr 6, 2012 10:16 PM   in reply to Marc Autret

    Anyway, let's try to make such routine work in any context, supporting any custom measurement units, rulers settings, rotated spreads and/or even scaled/skewed pages! To do so we need to avoid usual methods based on "geometric bounds". Interesting challenge!

    I think you might have scared him. Someone should demonstrate the easy way (I'll get to it...tomorrow?).

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (1)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points