我需要在perl上绘制2D线到1024x768的图像;线的坐标是unix时间戳,如x1 = 1365693813 y1 =(某个虚拟中心)x2 = 1365793815 y2 =(某个虚拟中心)。什么公式可以将现实生活中的coord缩放到图像空间?
答案 0 :(得分:1)
假设($x1,$y1)
和($x2,$y2)
定义了“视图窗口”的左下角和右上角,并且您有一个点,您希望将($x3,$y3)
标记为$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!