2012-02-13 105 views
1

我使用imagefilledpolygon畫(PHP)GIF圖像上的箭頭,我想使黑色概述的箭頭創建多邊形輪廓。我首先創建箭頭三角形,然後在該三角形的箭頭效果的基礎繪製厚線。爲GIF圖像

一個解決方案,我認爲是創建兩個三角箭頭,第一個是黑色的,比第一(創建大綱效果)略大,但我不知道如何去的是數學。

任何幫助將是偉大的!

代碼來創建箭頭(頭和尾):

function arrow($img, $x1, $y1, $x2, $y2, $alength, $awidth, $color) { 
    $distance = sqrt(pow($x1 - $x2, 2) + pow($y1 - $y2, 2)); 

    $dx = $x2 + ($x1 - $x2) * $alength/$distance; 
    $dy = $y2 + ($y1 - $y2) * $alength/$distance; 

    $k = $awidth/$alength; 

    $x2o = $x2 - $dx; 
    $y2o = $dy - $y2; 

    $x3 = $y2o * $k + $dx; 
    $y3 = $x2o * $k + $dy; 

    $x4 = $dx - $y2o * $k; 
    $y4 = $dy - $x2o * $k; 

    imagelinethick($img, $x1, $y1, $dx, $dy, $color,3); 
    return imagefilledpolygon($img, array($x2, $y2, $x3, $y3, $x4, $y4), 3, $color); 
} 

這個函數被調用的箭頭線(僅供參考):

function imagelinethick($image, $x1, $y1, $x2, $y2, $color, $thick = 1) 
{ 
    /* this way it works well only for orthogonal lines 
    imagesetthickness($image, $thick); 
    return imageline($image, $x1, $y1, $x2, $y2, $color); 
    */ 
    if ($thick == 1) { 
     return imageline($image, $x1, $y1, $x2, $y2, $color); 
    } 
    $t = $thick/2 - 0.5; 
    if ($x1 == $x2 || $y1 == $y2) { 
     return imagefilledrectangle($image, round(min($x1, $x2) - $t), round(min($y1, $y2) - $t), round(max($x1, $x2) + $t), round(max($y1, $y2) + $t), $color); 
    } 
    $k = ($y2 - $y1)/($x2 - $x1); //y = kx + q 
    $a = $t/sqrt(1 + pow($k, 2)); 
    $points = array(
     round($x1 - (1+$k)*$a), round($y1 + (1-$k)*$a), 
     round($x1 - (1-$k)*$a), round($y1 - (1+$k)*$a), 
     round($x2 + (1+$k)*$a), round($y2 - (1-$k)*$a), 
     round($x2 + (1-$k)*$a), round($y2 + (1+$k)*$a), 
    ); 
    imagefilledpolygon($image, $points, 4, $color); 
    return imagepolygon($image, $points, 4, $color); 
} 

回答

0

繪製一個稍大箭頭將是有問題的,因爲它不會在每一點上相同數量較大。可以繪製的箭頭輪廓顏色8次,通過對每個8個方向的1個像素的偏移,然後繪製在它上面的最終形狀。