Velocity Reviews > Perl > Just can't seem to figure out how to do this in perl

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

Bill H
Guest
Posts: n/a

 11-13-2008
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

Scott Bryce
Guest
Posts: n/a

 11-13-2008
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.

cartercc
Guest
Posts: n/a

 11-13-2008
On Nov 13, 6:46*am, Bill H <(E-Mail Removed)> 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

sln@netherlands.com
Guest
Posts: n/a

 11-13-2008
On Thu, 13 Nov 2008 03:46:12 -0800 (PST), Bill H <(E-Mail Removed)> 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"

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)

smallpond
Guest
Posts: n/a

 11-13-2008
On Nov 13, 6:46 am, Bill H <(E-Mail Removed)> 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.

sln@netherlands.com
Guest
Posts: n/a

 11-13-2008
On Thu, 13 Nov 2008 17:01:28 GMT, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

>On Thu, 13 Nov 2008 03:46:12 -0800 (PST), Bill H <(E-Mail Removed)> 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"

>
>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)
>