2010年5月28日 星期五

GD_week14

HTML的部份:

零件編號: 設計者姓名: 零件長度: 零件寬度:
PHP的部份:
<?php
// week14_action.php
// 處理 week14_form.htm 所送回來的表單資料,並藉以直接顯示出對應的圖檔

// 取得表單傳回來的變數值
$number = $_POST["number"];
$name = $_POST["name"];
$length = $_POST["length"];
$width = $_POST["width"];

// 以下開始根據所取得的表單資料 show 出圖檔
    /*由於處理程式,直接假設取值不會有問題,因此不使用任何 echo 指令,傳送文字檔資料,
      因此 output buffer 中不會有資料,因此無需使用 ob_clean()進行清除*/
    header("Content-type: image/png");
//1.設定圖片大小 --------------------------------------------------------//
    $im = imagecreatetruecolor($length,$width);
    //$bgcolor = ImageColorAllocate ($im, Red(0~255), Green(0~255), Blue(0~255));
//2.設定背景顏色填入淺藍色底色 ------------------------------------------//
    $bgcolor = ImageColorAllocate ($im, 153, 204, 255);
    imagefill($im, 0, 0, $bgcolor);
    
//3.用線來畫邊框 --------------------------------------------------------//
    $black = ImageColorAllocate ($im, 0, 0, 0);
    // 畫框的一些參數
    $thick = 20;
    $margin = 40;
    // 畫最上方平行線
    $startx = 0 + $margin;
    $starty = 0 + $margin;
    $endx = $length - $margin;
    $endy = $starty;
    //imagelinethick($image,x1,y1,x2,y2,顏色,粗細);
    imagelinethick($im,$startx,$starty,$endx,$endy,$black,$thick);
    
//4.寫上編號與姓名 ------------------------------------------------------//
    $string = "Number:".$number."\nName:".$name;
    $font = "fireflysung.ttf";
    //imagettftext(resource $image,大小,角度,X座標,Y座標,顏色,字型,文字)
    ImageTTFText($im, 20, 0, ($length/2)-40,($width/2)-10, $black, $font, $string);
    
//5.畫箭頭 --------------------------------------------------------------//
    $red = ImageColorAllocate ($im, 255, 0, 0);
    //arrow($image,x1,y1,x2,y2,箭頭長度,箭頭寬度,顏色);
    arrow($im, 150,150, 350,150, $length/50, $width/50, $red);
    
//6.結束繪圖 ------------------------------------------------------------//
    imagepng($im);
    imagedestroy($im);

//-----------------------------------------------------------------------

//以下函式直接取自 http://www.php.net/manual/en/function.imageline.php
function imagelinethick($image, $x1, $y1, $x2, $y2, $color, $thick = 1)
{
    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);
}

// 以下函式直接取自 http://php.net/manual/en/function.imageline.php
function arrow($im, $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($im, $x1, $y1, $dx, $dy, $color,3);
    imagefilledpolygon($im, array($x2, $y2, $x3, $y3, $x4, $y4), 3, $color);
}
?>

2010年5月18日 星期二

河內塔(Hanoi tower)

不知道河內塔是什麼嗎?請點我

遊戲規則:
1.每次只能移動一個盤子
2.大盤子不可以在小盤子的上面

程式碼:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

void HNTower(int,char,char,char);
int main() {
    int n;
    char A = 'A',B = 'B',C = 'C';
    scanf("%d",&n);
    HNTower(n,A,B,C);
    system("PAUSE");
    return 0;
}
void HNTower(int n,char a,char b,char c) {
    if(n == 1)
        printf("Move dish no.1 from %c to %c\n",a,c);
    else{
        HNTower(n - 1,a,c,b);
        printf("Move dish no.%d from %c to %c\n",n,a,c);
        HNTower(n - 1,b,a,c);
    }
}
參考看看吧。

 

2010年5月13日 星期四

簡易不弱格貼紙製造機

這程式沒有寫得很好僅供參考,

這個程式已經改好了=w=+

附加檔案:這裡

簡易不弱格貼紙製造機原始碼:

HTM部分:

<!--程式名稱:index.htm-->
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><!--宣告文件型態為萬國碼-->
<form method="GET" action="gd_change.php" enctype="multipart/form-data">
<!--文字輸入框-->
輸入一個字:<input name="word" type="text"
style="text-align:center; border-top-style:none; border-right-style:none; border-left-style:none;"
value="傻" size="1" /><br />
<!--size="1" 指定要多大的框框-->
<!--text-align:center; 文字置中-->
<!--border-top-style:none; 隱藏框框的上面的線-->
<!--border-right-style:none; 隱藏右邊的線-->
<!--border-left-style:none; 隱藏左邊的線-->

<!--下拉式選單-->
請選擇顏色:
<select name="color"><!--下拉式選單的名稱-->
<option value="r_eye">紅色</option><!--選項們-->
<option value="b_eye">藍色</option>
<option value="g_eye">綠色</option>
<option value="y_eye">棕色</option>
<option value="bk_eye">黑色</option>
</select>
<input type="hidden" name="action" value="1">
<!--送出按鈕-->
<input type="submit" value="送出"></form>


PHP部分:

<?php
//程式名稱:gd_change.php
$act = $_GET["action"];
$color = $_GET["color"];//讀進顏色
$string = $_GET["word"];//讀進文字
if($act && $color && $string) {
 header("Content-type: image/jpeg");//宣告文件型態
 $im = imagecreatefromjpeg($color.".jpg");//圖片檔案
 $fontsize = 8;//文字大小
 $angle = 0;//文字角度
 $x = 35;//文字X座標
 $y = 26;//文字Y座標
 $color = imagecolorallocate($im, 255, 255, 255);//文字顏色
 $font = "../fonts/fireflysung.ttf";//文字TTF字型
 ImageTTFText($im, $fontsize, $angle, $x, $y, $color, $font, $string);//輸出文字
 imagejpeg($im);//輸出圖形
 imagedestroy($im);//結束圖形編輯
}
?>

2010年5月6日 星期四

atoi整數轉換+強制轉換成int型態

//下面是範例程式

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int intnum;
    double doublenum;
    char word[30]; //最多只能讀30個字母組成的單字
    while(1)
    {
        printf("\nEnter any number:");
        scanf("%s",word);
        // 呼叫 atoi()將字串轉為整數,掃到非數字值即終止
        // 如輸入『123abc456』則會輸出『123』
        doublenum = atoi(word);
        // 強制轉換成int
        intnum = (int)doublenum;
        if(intnum<=50 && intnum>0)
        {
            printf("%d is >0 and <= 50",intnum);
        }
        else if(intnum == 0)
        {
            break;
        }
        else
        {
            printf("%d is <=0 or not integer",intnum);
        }
    }
    return 0;
}

GDpng程式

<?php
//程式名稱:gdpngtext.php
header("Content-type: image/png");

$im = imagecreatefrompng("test.png");//圖片檔案
$fontsize = 14;//文字大小
$angle = 0;//文字角度
$x = 25;//文字X座標
$y = 80;//文字Y座標
$color = imagecolorallocate($im, 255, 255, 255);//文字顏色
$font = "fireflysung.ttf";//文字TTF字型
$string = "國立虎尾科技大學";//文字

ImageTTFText($im, $fontsize, $angle, $x, $y, $color, $font, $string);//輸出文字
ImagePng($im);//輸出圖形
ImageDestroy($im);//結束圖形編輯
?>