everycode2

第二期:用php画国旗

每日代码汇第二期:php版国旗,原来程序员有时候也可以是画家!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
$Nationflag = ImageCreate(660, 440);
ImageColorAllocate ($Nationflag,255,0,0);
DrawPentacle($Nationflag,180,150,120,120);
DrawPentacle($Nationflag,80,30,210,50);
DrawPentacle($Nationflag,120,30,250,100);
DrawPentacle($Nationflag,180,30,250,160);
DrawPentacle($Nationflag,100,30,210,210);
Header('Content-type: image/png');
ImagePng($Nationflag);
ImageDestroy($Nationflag);

/*
    函数:绘制五角星
    参数
        $mFlag:图像标识
        $mAngle:旋转角度
        $mRadius:外接圆半径
        $position_X:绘制坐标X
        $position_Y:绘制坐标Y
*/

function DrawPentacle($mFlag,$mAngle,$mRadius,$position_X,$position_Y){
    $yellow = imagecolorallocate($mFlag, 255, 255, 0);
    $PI = 3.14;
    $theta=54+$mAngle;
    for ($i=0;$i&lt;5;$i++) {//五顶点坐标 
        $r = $mRadius*sin(18*$PI/180)/sin(54*$PI/180);
        $points[$i]= array('x'=>$r*cos(($theta+$i*72)*$PI/180)+$position_X,'y'=>-$r*sin(($theta+$i*72)*$PI/180)+$position_Y);
    }      
    for($i=0;$i&lt;5;$i++){//五条线坐标
        $j=$i>2?$i-3:$i+2;
        $lines[$i] = array('x1' => $points[$i]['x'], 'y1' => $points[$i]['y'], 'x2' => $points[$j]['x'], 'y2' => $points[$j]['y']);
    }
    for($i=0;$i&lt;5;$i++){//五交点坐标   
        $j = $i>3?0:$i+1;
        $x12 = $lines[$i]['x1'] - $lines[$i]['x2'];
        $x34 = $lines[$j]['x1'] - $lines[$j]['x2'];
        $y12 = $lines[$i]['y1'] - $lines[$i]['y2'];
        $y34 = $lines[$j]['y1'] - $lines[$j]['y2'];
        $c = $x12 * $y34 - $y12 * $x34;
        $a = $lines[$i]['x1'] * $lines[$i]['y2'] - $lines[$i]['y1'] * $lines[$i]['x2'];
        $b = $lines[$j]['x1'] * $lines[$j]['y2'] - $lines[$j]['y1'] * $lines[$j]['x2'];
        $x = ($a * $x34 - $b * $x12) / $c;
        $y = ($a * $y34 - $b * $y12) / $c;
        $intersection[$i]= array('x'=>$x,'y'=>$y);
    }
    for($i=0;$i&lt;3;$i++){//三个三角形
        $Polygon[$i] = array($points[$i]['x'],$points[$i]['y'],$points[$i+2]['x'],$points[$i+2]['y'],$intersection[$i+2]['x'],$intersection[$i+2]['y']);   
        imagefilledpolygon($mFlag, $Polygon[$i], 3, $yellow);
    }
   
}
?>

图例:

来源:http://www.oschina.net/code/snippet_817765_15060(oschina.net)

生产:八宝粥

主页:http://my.oschina.net/funnky

分享到:

6 条评论

昵称
  1. 短线炒股技巧

    [鸭梨很大] 不错,值得收藏分享!

  2. 亚马逊美国

    [冻结] 支持下,越来越好希望

  3. 全球速卖通

    [康乃馨] 威武啊。。。。

  4. 高桥腹滴

    [鸭梨很大]

  5. Louis Han

    哈 还真是有创意啊

    1. 淘宝客是什么

      技术不是吹出来的!!! [给力]