2 codelearning codelearning 于 2014.11.02 16:01 提问

c程序转换为java程序,最好用图形界面实现

#include
#include
#define N 50

#define M 3.14159265

void erwei();
void sanwei();

void main()
{
int w;
printf("请输入是几维图形变换:二维(2)或三维(3): ");
scanf_s("%d", &w);
if(w == 2) erwei();
else if(w == 3) sanwei();
}

void erwei()
{
float a[3][3] = { { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 } };
int ii, k, h;
float b[N][2];
printf("请输入图形定点个数 : ");
scanf_s("%d", &k);
printf("请输入顶点坐标:\n");
for (ii = 0; ii<k; ii++)
scanf_s("%f,%f", &b[ii][0], &b[ii][1]);
printf("请输入变换模式:比例(1),对称(2),错切(3),平移(4),旋转(5): ");
scanf_s("%d", &h);
if (h == 1)
{
int m, n;
printf("请输入比例因子(x,y): ");
scanf_s("%d,%d", &m, &n);
a[0][0] = m;
a[1][1] = n;
}
else if (h == 2)
{
int ch;
printf("请输入对称轴:x(1)或y(2): ");
scanf_s("%d", &ch);
if (ch == 1) a[1][1] = -1;
if (ch == 2) a[0][0] = -1;
}
else if (h == 3)
{
int r;
float f;
printf("请输入错切轴x(1)或y(2)和错切因子a(b): ");
scanf_s("%d,%f", &r, &f);
if (r == 1)
a[1][0] = f;
if (r == 2)
a[0][1] = f;
}
else if (h == 4)
{
float i, j;
printf("请输入平移坐标(x,y): ");
scanf_s("%f,%f", &i, &j);
a[2][0] = i; a[2][1] = j;
}
else if (h == 5)
{
float g;
printf("请输入旋转角度(度): ");
scanf_s("%f", &g);
g = M*g / 180;
a[0][0] = cos(g);
a[0][1] = sin(g);
a[1][0] = -sin(g);
a[1][1] = cos(g);
}
printf("经过变换后的坐标为:\n");
for (ii = 0; ii<k; ii++)
printf("%.2f,%.2f\n", b[ii][0] * a[0][0] + b[ii][1] * a[1][0] + a[2][0], b[ii][0] * a[0][1] + b[ii][1] * a[1][1] + a[2][1]);
}

void sanwei()
{
float a[4][4] = { { 1, 0, 0, 0 }, { 0, 1, 0, 0 }, { 0, 0, 1, 0 }, { 0, 0, 0, 1 } };
float c[N][3], x1, y1, z1; int h, q, jj;
printf("请输入三维图形的顶点个数: ");
scanf_s("%d", &q);
printf("请输入顶点坐标: \n");
for (jj = 0; jj<q; jj++)
scanf_s("%f,%f,%f", &c[jj][0], &c[jj][1], &c[jj][2]);
printf("请输入变换模式:比例(1),对称(2),错切(3),平移(4),旋转(5): ");
scanf_s("%d", &h);
if (h == 1)
{
int l, m, n;
printf("请输入比例因子(x,y,z): ");
scanf_s("%d,%d,%d", &l, &m, &n);
a[0][0] = l;
a[1][1] = m;
a[2][2] = n;
}
else if (h == 2)
{
int ch;
printf("请输入对称面:xy(1),yz(2),zx(3): ");
scanf_s("%d", &ch);
if (ch == 1) a[2][2] = -1;
if (ch == 2) a[0][0] = -1;
if (ch == 3) a[1][1] = -1;
}
else if (h == 3)
{
int str;
float o, p;
printf("请输入错切面:xy(1),yz(2),zx(3): ");
scanf_s("%d", &str);
printf("请输入错切因子(a,b): ");
scanf_s("%f,%f", &o, &p);
if (str == 1) {
a[1][0] = o;
a[2][0] = p;
a[0][1] = o;
a[2][1] = p;
} if (str == 2)
{
a[0][1] = o;
a[2][1] = p;
a[0][2] = o;
a[1][2] = p;
} if (str == 3)
{
a[1][0] = o;
a[2][0] = p;
a[0][2] = o;
a[1][2] = p;
}
}
else if (h == 4)
{
float r, s, t;
printf("请输入平移坐标(x,y,z): ");
scanf_s("%f,%f,%f", &r, &s, &t);
a[3][0] = r;
a[3][1] = s;
a[3][2] = t;
}
else if (h == 5)
{
int e;
float du;
printf("请输入旋转轴,x轴(1),y轴(2),z轴(3): ");
scanf_s("%d", &e);
printf("请输入旋转角度(度): ");
scanf_s("%f", &du);
du = M*du / 180;
if (e == 1){
a[1][1] = cos(du);
a[2][1] = -sin(du);
a[1][2] = sin(du); a[2][2] = cos(du);
}
if (e == 2)
{
a[0][0] = cos(du);
a[2][0] = -sin(du);
a[0][2] = sin(du);
a[2][2] = cos(du);
}
if (e == 3)
{
a[0][0] = cos(du);
a[1][0] = -sin(du);
a[0][1] = sin(du);
a[1][1] = cos(du);
}
}
printf("经变换后坐标为:\n");
for (jj = 0; jj<q; jj++)
{
x1 = c[jj][0] * a[0][0] + c[jj][1] * a[1][0] + c[jj][2] * a[2][0] + a[3][0];
y1 = c[jj][0] * a[0][1] + c[jj][1] * a[1][1] + c[jj][2] * a[2][1] + a[3][1];
z1 = c[jj][0] * a[0][2] + c[jj][1] * a[1][2] + c[jj][2] * a[2][2] + a[3][2];
printf("%.2f,%.2f,%.2f\n", x1, y1, z1);
}
}

Csdn user default icon
上传中...
上传图片
插入图片