root/samples/mikk_ni3_92/bresenham.php

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. __construct
  2. GLUT_INIT
  3. GLUT_CALL_FUNC
  4. MY_INIT
  5. display
  6. reshape
  7. mouse
  8. CALC_DATA
  9. DRAW_LINE

<?php

class Point {
    public $x, $y;
    function __construct($x, $y) {
        $this->x = $x;
        $this->y = $y;
    }
}

$a = new Point(30, 100);
$b = new Point(200, 250);


/***[計算用パラメータ]***/
class PARAM {
    public $dx, $dy;
    public $a, $a1, $e;
    public $sx, $sy;
    public $flag;
};

$param = new PARAM();


/******[OpenGLの初期設定]******************/
function GLUT_INIT()
{
    glutInitDisplayMode(GLUT_RGBA);
    glutInitWindowSize(400, 400);
}


function GLUT_CALL_FUNC()
{
    glutDisplayFunc('display');
    glutReshapeFunc('reshape');
    glutMouseFunc('mouse');
}

function MY_INIT()
{
    glClearColor(1.0, 1.0, 1.0, 1.0);
    CALC_DATA();
}


// Main

glutInit($argv);
GLUT_INIT();

glutCreateWindow("window name");

GLUT_CALL_FUNC();
MY_INIT();
glutMainLoop();

exit(0);

function display()
{
    global $param;
    glColor3d(1, 0, 0);
    DRAW_LINE($param->flag);
    glFlush();
}

function reshape($w, $h)
{
    glClear(GL_COLOR_BUFFER_BIT);
    glViewport(0, 0, $w, $h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0, $w, $h, 0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}


function mouse($button, $state, $x, $y)
{
    if( $button == GLUT_LEFT_BUTTON && $state == GLUT_DOWN)
    {
        printf("click at (%d,%d) \n", $x, $y);
    }
}


/*******[ここから、各種関数]******************/

/* 2点から必要なデータを計算 */
function CALC_DATA()
{
    global $a, $b;
    global $param;
    $param->dx = abs($a->x - $b->x);
    $param->dy = abs($a->y - $b->y);

    $a->x > $b->x ? ($param->sx = -1) : $param->sx = 1;
    $a->y > $b->y ? ($param->sy = -1) : $param->sy = 1;

    if ($param->dx >= $param->dy) {
        $param->a = 2 * $param->dy;
        $param->a1 = $param->a - (2 * $param->dx);
        $param->e = $param->a - $param->dx;
        $param->flag = 0;
    } else {
        $param->a = 2 * $param->dx;
        $param->a1 = $param->a - (2 * $param->dy);
        $param->e = $param->a - $param->dy;
        $param->flag = 1;
    }
}


/* プレゼンハムで描画 */
function DRAW_LINE($flag)
{
    global $a, $b;
    global $param;
    $x = $a->x;
    $y = $a->y;
    if($param->flag == 0) { //xで走査
        for($x = $a->x ; ($param->sx >=0 ? $x<= $b->x : $x>=$b->x) ; $x+=$param->sx) {
            glBegin(GL_POINTS);
            glVertex2i($x, $y);
            glEnd();
            if($param->e >= 0) {
                $y += $param->sy;
                $param->e += $param->a1;
            } else {
                $param->e += $param->a;
            }
        }
    } else { //yで走査
        for ($y = $a->y; ($param->sy >=0 ? $y<= $b->y : $y>=$b->y) ; $y+=$param->sy) {
            glBegin(GL_POINTS);
            glVertex2i($x, $y);
            glEnd();
            if ($param->e>=0) {
                $x += $param->sx;
                $param->e += $param->a1;
            } else {
                $param->e += $param->a;
            }
        }
    }
}

/* [<][>][^][v][top][bottom][index][help] */