Just can't seem to figure out how to do this in perl

Discussion in 'Perl Misc' started by Bill H, Nov 13, 2008.

  1. Bill H

    Bill H Guest

    I have been trying to figure out how to do this in perl for weeks and
    justcan't seem to fingure it out. I can see it in my mind how it
    should work.

    The problem I have is this. I have a flash interface that sends perl
    some image coordinates that perl then uses to make pdf's using
    pdf:api2. In flash I use the standard coordinate system:

    0,0------------------> X
    |
    |
    |
    v
    Y

    (ie x,y is starts at top left corner). So I will send the perl program
    a image coordinate in the format of x,y,width,height.

    Now on the prl side, making the pdf the coordinate system is:

    Y
    |
    |
    |
    |
    0,0 ------------------> X

    (ie x,y starts at bottom left corner) and I need to translate the
    flash coordinates to the perl / pdf coordinates.

    So, if I have a page that is 5 1/2" x 8 1/2" inches, or 396 x 612
    (using 72dpi) and I am placing a 1" x 1" image at 1" x 1" (x,y = 72,72
    in flash and width = 72, and height = 72) then my x,y coordinates in
    perl whould be 72,540.

    The problem I have is, trying to come up with a algorythm that will
    translate the one set of coordinates into the other.

    I hope I am making sense.

    Bill H
    Bill H, Nov 13, 2008
    #1
    1. Advertising

  2. Bill H

    Scott Bryce Guest

    Bill H wrote:
    <snipped description of problem translating from screen coordinates to
    PDF page coordinates>

    > The problem I have is, trying to come up with a algorythm that will
    > translate the one set of coordinates into the other.


    Assuming the resolution of the PDF page is the same as the resolution of
    the Flash object...


    $pdf_x_position = $flash_x_position;
    $pdf_y_position = $pdf_page_height - $flash_y_position;

    But images in Flash are positioned by the upper left corner (I assume)
    and images in PDF are positioned by the lower left corner, so you need
    to adjust for that as well.

    $pdf_y_position = $pdf_page_height - $flash_y_position - $image_size;

    You may also need to adjust for differing resolutions.

    $pdf_x_position = $flash_x_position / $flash_object_width * $pdf_page_width;

    $pdf_y_position = ($pdf_page_height - $flash_y_position - $image_size) /
    $flash_object_height * $pdf_page_height;


    I haven't tested any of that, but I think I have it right.
    Scott Bryce, Nov 13, 2008
    #2
    1. Advertising

  3. Bill H

    cartercc Guest

    On Nov 13, 6:46 am, Bill H <> wrote:
    > So, if I have a page that is 5 1/2" x 8 1/2" inches, or 396 x 612
    > (using 72dpi) and I am placing a 1" x 1" image at 1" x 1" (x,y = 72,72
    > in flash and width = 72, and height = 72) then my x,y  coordinates in
    > perl whould be 72,540.
    >
    > The problem I have is, trying to come up with a algorythm that will
    > translate the one set of coordinates into the other.


    Perfect sense. I have also struggled with PDF and Perl, so I
    sympathize.

    Since the PDF placement is measured from the bottom, subtract the
    object height from the page height. IOW, if the page height is 612 and
    the object height (measured from the top) is 72, 612 - 72 = 540.

    CC
    cartercc, Nov 13, 2008
    #3
  4. Bill H

    Guest

    On Thu, 13 Nov 2008 03:46:12 -0800 (PST), Bill H <> wrote:

    >I have been trying to figure out how to do this in perl for weeks and
    >justcan't seem to fingure it out. I can see it in my mind how it
    >should work.
    >
    >The problem I have is this. I have a flash interface that sends perl
    >some image coordinates that perl then uses to make pdf's using
    >pdf:api2. In flash I use the standard coordinate system:
    >
    >0,0------------------> X
    >|
    >|
    >|
    >v
    >Y
    >
    >(ie x,y is starts at top left corner). So I will send the perl program
    >a image coordinate in the format of x,y,width,height.
    >
    >Now on the prl side, making the pdf the coordinate system is:
    >
    >Y
    >|
    >|
    >|
    >|
    >0,0 ------------------> X
    >
    >(ie x,y starts at bottom left corner) and I need to translate the
    >flash coordinates to the perl / pdf coordinates.
    >
    >So, if I have a page that is 5 1/2" x 8 1/2" inches, or 396 x 612
    >(using 72dpi) and I am placing a 1" x 1" image at 1" x 1" (x,y = 72,72
    >in flash and width = 72, and height = 72) then my x,y coordinates in
    >perl whould be 72,540.
    >
    >The problem I have is, trying to come up with a algorythm that will
    >translate the one set of coordinates into the other.
    >
    >I hope I am making sense.
    >
    >Bill H


    I don't know why you try to obfucate the issue with
    > ... and I am placing a 1" x 1" image at 1" x 1"

    Whats that all about?

    You can do something complicated like this:

    sln
    ----------------------------

    use strict;
    use warnings;

    sub ConvertInchesToDots
    {
    my ($dpi, $inchref) = @_;
    my @dots = ();
    for (@$inchref) {
    push @dots, ($_ * $dpi);
    }
    return \@dots;
    }
    sub FlashToPdfCoords
    {
    my ($dpi, $refFlashCoords) = @_;
    my $refpdfcoords = ConvertInchesToDots ($dpi, $refFlashCoords);
    $refpdfcoords->[1] = $refpdfcoords->[3] - $refpdfcoords->[1]; # pdfY = dpi * (flashH - flashY)
    return @$refpdfcoords;
    }


    my @Flashcoords = (1, 1, 5.5, 8.5); # x,y,width,height
    my @Pdfcoords = FlashToPdfCoords (72, \@Flashcoords);

    print "\nPdf ( X, Y, Width, Heigth ) = Flash ( X, Y, Width, Heigth )\n";
    print "Pdf (@Pdfcoords) = Flash (@Flashcoords)\n";


    __END__


    Pdf ( X, Y, Width, Heigth ) = Flash ( X, Y, Width, Heigth )
    Pdf (72 540 396 612) = Flash (1 1 5.5 8.5)
    , Nov 13, 2008
    #4
  5. Bill H

    smallpond Guest

    On Nov 13, 6:46 am, Bill H <> wrote:
    > I have been trying to figure out how to do this in perl for weeks and
    > justcan't seem to fingure it out. I can see it in my mind how it
    > should work.
    >
    > The problem I have is this. I have a flash interface that sends perl
    > some image coordinates that perl then uses to make pdf's using
    > pdf:api2. In flash I use the standard coordinate system:
    >
    > 0,0------------------> X
    > |
    > |
    > |
    > v
    > Y
    >
    > (ie x,y is starts at top left corner). So I will send the perl program
    > a image coordinate in the format of x,y,width,height.
    >
    > Now on the prl side, making the pdf the coordinate system is:
    >
    > Y
    > |
    > |
    > |
    > |
    > 0,0 ------------------> X
    >
    > (ie x,y starts at bottom left corner) and I need to translate the
    > flash coordinates to the perl / pdf coordinates.
    >
    > So, if I have a page that is 5 1/2" x 8 1/2" inches, or 396 x 612
    > (using 72dpi) and I am placing a 1" x 1" image at 1" x 1" (x,y = 72,72
    > in flash and width = 72, and height = 72) then my x,y coordinates in
    > perl whould be 72,540.
    >
    > The problem I have is, trying to come up with a algorythm that will
    > translate the one set of coordinates into the other.
    >
    > I hope I am making sense.
    >
    > Bill H


    No need to reinvent the wheel.

    The CPAN module to do this generally is called
    Geometry::AffineTransform.
    You set up a matrix to do any 2D -> 2D coordinate transform, which
    will let you scale/convert units, rotate, translate, etc.
    If your units are inches and you want to convert to points then you
    are
    scaling by 72, for example.
    smallpond, Nov 13, 2008
    #5
  6. Bill H

    Guest

    On Thu, 13 Nov 2008 17:01:28 GMT, wrote:

    >On Thu, 13 Nov 2008 03:46:12 -0800 (PST), Bill H <> wrote:
    >
    >>I have been trying to figure out how to do this in perl for weeks and
    >>justcan't seem to fingure it out. I can see it in my mind how it
    >>should work.
    >>
    >>The problem I have is this. I have a flash interface that sends perl
    >>some image coordinates that perl then uses to make pdf's using
    >>pdf:api2. In flash I use the standard coordinate system:
    >>
    >>0,0------------------> X
    >>|
    >>|
    >>|
    >>v
    >>Y
    >>
    >>(ie x,y is starts at top left corner). So I will send the perl program
    >>a image coordinate in the format of x,y,width,height.
    >>
    >>Now on the prl side, making the pdf the coordinate system is:
    >>
    >>Y
    >>|
    >>|
    >>|
    >>|
    >>0,0 ------------------> X
    >>
    >>(ie x,y starts at bottom left corner) and I need to translate the
    >>flash coordinates to the perl / pdf coordinates.
    >>
    >>So, if I have a page that is 5 1/2" x 8 1/2" inches, or 396 x 612
    >>(using 72dpi) and I am placing a 1" x 1" image at 1" x 1" (x,y = 72,72
    >>in flash and width = 72, and height = 72) then my x,y coordinates in
    >>perl whould be 72,540.
    >>
    >>The problem I have is, trying to come up with a algorythm that will
    >>translate the one set of coordinates into the other.
    >>
    >>I hope I am making sense.
    >>
    >>Bill H

    >
    >I don't know why you try to obfucate the issue with
    >> ... and I am placing a 1" x 1" image at 1" x 1"

    >Whats that all about?
    >
    >You can do something complicated like this:
    >
    >sln
    >----------------------------
    >
    >use strict;
    >use warnings;
    >
    >sub ConvertInchesToDots
    >{
    > my ($dpi, $inchref) = @_;
    > my @dots = ();
    > for (@$inchref) {
    > push @dots, ($_ * $dpi);

    ^^^^^^^^^^^
    push @dots, (int(($_ * $dpi)+.5));

    Forgot this. This will round then convert fractional inches to integer dots

    > }
    > return \@dots;
    >}
    >sub FlashToPdfCoords
    >{
    > my ($dpi, $refFlashCoords) = @_;
    > my $refpdfcoords = ConvertInchesToDots ($dpi, $refFlashCoords);
    > $refpdfcoords->[1] = $refpdfcoords->[3] - $refpdfcoords->[1]; # pdfY = dpi * (flashH - flashY)
    > return @$refpdfcoords;
    >}
    >
    >
    >my @Flashcoords = (1, 1, 5.5, 8.5); # x,y,width,height
    >my @Pdfcoords = FlashToPdfCoords (72, \@Flashcoords);
    >
    >print "\nPdf ( X, Y, Width, Heigth ) = Flash ( X, Y, Width, Heigth )\n";
    >print "Pdf (@Pdfcoords) = Flash (@Flashcoords)\n";
    >
    >
    >__END__
    >
    >
    >Pdf ( X, Y, Width, Heigth ) = Flash ( X, Y, Width, Heigth )
    >Pdf (72 540 396 612) = Flash (1 1 5.5 8.5)
    >
    , Nov 13, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. logia
    Replies:
    6
    Views:
    640
  2. Chad A. Beckner

    Please Help! Can't figure this out...

    Chad A. Beckner, Jun 18, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    391
    bloomfield
    Jun 21, 2004
  3. myleslawrence

    Simple but I can't figure it out

    myleslawrence, Jan 19, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    313
    IPGrunt
    Jan 19, 2005
  4. Replies:
    2
    Views:
    3,277
    Ron Natalie
    Aug 11, 2006
  5. Albert Hopkins
    Replies:
    3
    Views:
    463
    Steven D'Aprano
    Dec 10, 2008
Loading...

Share This Page