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

B

Bill H

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
 
S

Scott Bryce

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.
 
C

cartercc

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
 
S

sln

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

smallpond

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.
 
S

sln

I don't know why you try to obfucate the issue with
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)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,817
Latest member
AdalbertoT

Latest Threads

Top