0

我需要在 perl 上绘制 2D 线到 1024x768 大小的图像中;线的坐标是 unix 时间戳,例如 x1=1365693813 y1=(some virtual center) x2=1365793815 y2=(some virtual center)。将现实生活中的坐标缩放到图像空间的公式是什么?

4

1 回答 1

1

假设($x1,$y1)($x2,$y2)定义“视图窗口”的左下角和右上角,并且您有一个点要绘制($x3,$y3)where$x1 <= $x3 <= $x2$y1 <= $y3 <= $y2。还假设您在标准图像空间中工作,其中 (0,0) 是图像的左上角。您可以找到($xp, $yp)像这样在图像上绘制的像素坐标:

# View window
my ($x1,$y1) = (1365693813, 100);
my ($x2,$y2) = (1365693815, 200);
my ($vw ,$vh) = ( $x2 - $1, $y2 - $y1 );

# Image width/height
my ($imgw,$imgh) = (1024, 768);

# Point to plot in original co-ordinates
my ($x3,$y3) = (1365693814, 150);

# Calculate point to plot in image co-ordinates
my $xp = int( ( $imgw * ($x3 - $x1)/$vw) + 0.5 );
my $yp = int( ( $imgh * ( 1.0 - ($y3 - $y1)/$vh) ) + 0.5 );

# Now plot ( $xp, $yp ), provided it is inside the graphic!
于 2013-04-11T12:32:39.303 回答