2011-10-25 4 views
0

挨拶、点の周りに三角形を移動して回転させない - C Bgiグラフィック

私はBGIグラフィックのこのグラフィックの宿題を持っています。私たちはDevCPPとBGIと行列を使わなければなりません。

私はこのコードを書いています。私は変換が良いと思います。しかし、私の三角形は円の周りを動いたり回転したりしません。なぜか円の周りを動かないのは分かりません...

私はどこで何を書き直さなければならないのですか?

#include <math.h> 
#include "graphics.h" 
#include <stdio.h> 
#include <stdlib.h> 
#include <conio.h> 

#define PI 3.14159265 

typedef float Matrix3x3[3][3]; 
Matrix3x3 theMatrix; 

int Round(double n){ 
    return (int)(n + 0.5); 
} 

void matrix3x3SetIdentity(Matrix3x3 m) 
{ 
    int i, j; 
    for(i=0; i<3; i++) 
       for(j=0; j<3;j++) 
         m[i][j]=(i==j); 
} 

/* Multiplies matrix, result in b matrix */ 
void matrix3x3PreMultiply(Matrix3x3 a, Matrix3x3 b) 
{ 
    int r, c; 
    Matrix3x3 tmp; 

    for(r=0; r<3;r++) 
     for(c=0; c<3;c++) 
     tmp[r][c]= 
      a[r][0]*b[0][c]+a[r][1]*b[1][c]+a[r][2]*b[2][c]; 

    for(r=0; r<3;r++) 
     for(c=0; c<3; c++) 
     b[r][c]-tmp[r][c]; 

} 

void translate2(int tx, int ty) 
{ 
    Matrix3x3 m; 

    matrix3x3SetIdentity (m); 
    m[0][2] = tx; 
    m[1][2] = ty; 
    matrix3x3PreMultiply(m, theMatrix); 
} 

void scale2 (float sx, float sy, pont2d refpt) 
{ 
Matrix3x3 m; 
matrix3x3SetIdentity(m); 
m[0][0]=sx; 
m[0][2]=(1-sx)*refpt.x; 
m[1][1]=sy; 
m[1][2]=(1-sy)*refpt.y; 
matrix3x3PreMultiply(m, theMatrix); 
} 


void rotate2 (float a, pont2d refpt) 
{ 
    Matrix3x3 m; 
    matrix3x3SetIdentity(m); 
    a=a/PI; 
    m[0][0] = cosf(a); 
    m[0][1] = -sinf(a); 
    m[0][2] = refpt.x * (1-cosf(a)) + refpt.y * sinf(a); 
    m[1][0] = sinf (a); 
    m[1][1] = cosf (a); 
    m[1][2] = refpt.y * (1-cosf(a)) - refpt.x * sinf(a); 
    matrix3x3PreMultiply(m, theMatrix); 
} 

void transformPoints2 (int npts, pont2d *pts) 
{ 
    int k; 
    float tmp; 

    for (k = 0; k < npts; k++) { 
    tmp = theMatrix[0][0] * pts[k].x + theMatrix[0][1] * 
     pts[k].y + theMatrix[0][2]; 
    pts[k].y = theMatrix[1][0] * pts[k].x + theMatrix[1][1] * 
     pts[k].y + theMatrix[1][2]; 
    pts[k].x = tmp; 
    } 
} 


int main() 
{ 
int gd, gm, i, page=0; 
gd=VGA;gm=VGAHI; 
initgraph(&gd,&gm,""); 
int ap; 

while(!kbhit()) 
{ 
    setactivepage(page); 
    cleardevice(); 


    pont2d P[3] = { 50.0, 50.0, 150.0, 50.0, 100.0, 150.0}; 
    pont2d refPt = {200.0, 250.0}; 

    // Drawing the Triangle 
    moveto(Round(P[ 0 ].x), Round(P[ 0 ].y)); 
    for(i = 1; i < 3; i++) 
      lineto(Round(P[ i ].x), Round(P[ i ].y));   
    lineto(Round(P[ 0 ].x), Round(P[ 0 ].y)); 

    // Drawing the Circle 
    fillellipse(200, 250, 5,5); 
    setcolor (BLUE); 
    matrix3x3SetIdentity (theMatrix); 
    scale2 (0.5, 0.5, refPt); 
    //scale2 (20, 20, refPt); 
    rotate2 (90.0, refPt); 
    translate2 (0, 150); 
    transformPoints2 (3, P); 

    setvisualpage(page); 
    page = 1-page;  

} 

getch(); 

closegraph(); 

return 0; 

} 

答えて

1

オブジェクトを「回転」させたい場合は、ローカル原点について回転を実行する必要があります。グローバルな起点についての回転は、オブジェクトがグローバルな起点を「周回」する原因になります。元の位置に戻って変換の順序hereに関する議論で

ルックオブジェクトを回転

  • を適用

    1. は、グローバル原点
    2. にオブジェクトを翻訳翻訳:このように、オブジェクトを回転します説明のために。具体的には、「変容命令の重要性の実証」という節を探してください。

  • 0

    、三角形を回転させる三点を取得し、式を使用する:

    X '= X + R COS(シータ) Y' = Y - R罪(シータ)

    上記式0〜360のループに適用することができます。ループに遅延(200)ミリ秒を入れてグラフィックスシミュレーションを行うことができます。

    関連する問題