不围绕点移动和旋转三角形 - C Bgi 图形

发布于 12-11 23:53 字数 2841 浏览 1 评论 0原文

你好,

我有华大图形的图形作业。我们必须使用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;

}

Greeting,

I have this graphic homework in BGI graphic. We must use DevCPP and BGI, and matrices.

I wrote this code, and I think the transformations is good. But my triangle doesn't move and rotate around the circle, And I don't understand, why not it moves around the circle...

I don't know where and what I have to rewrite.

#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;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妄断弥空2024-12-18 23:53:21

如果您想看到对象“旋转”,则应围绕本地原点进行旋转。绕全局原点旋转将导致对象“绕”全局原点“运行”。因此,要旋转对象:

  1. 将对象平移到全局原点
  2. 应用旋转
  3. 将对象平移回其原始位置

查看有关变换顺序的讨论 此处 进行说明。具体来说,请查找标题为“演示转换顺序的重要性”的部分。

If you want to see the object "spin", then rotations should be performed about the local origin. Rotation about the global origin will cause the object to "orbit" the global origin. Thus, to spin the object:

  1. Translate the object to global origin
  2. Apply the rotation
  3. Translate the object back to its original position

Look at the discussion regarding transformation order here for an illustration. Specifically, look for the section entitled "Demonstration of the importance of transformation order".

作死小能手2024-12-18 23:53:21

要旋转三角形,请获取三个点并使用公式:

x' = x + r cos (theta)
y' = y - r sin (theta)

上面的公式可以应用于 0 到 360 的循环中。您可以通过在循环中放置延迟 (200) 毫秒来进行图形模拟。

To rotate a triangle, get the three points and use the formula:

x' = x + r cos (theta)
y' = y - r sin (theta)

The above formula can be applied into a loop where there being 0 to 360. You can have a graphics simulation by putting a delay (200) milliseconds in the loop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文