【问题标题】:C graphics program generating static output生成静态输出的 C 图形程序
【发布时间】:2015-12-22 14:50:00
【问题描述】:

我写了一个简单的代码来旋转一条线。以下是源代码:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
#include<graphics.h>
  void main(){
  int gd=DETECT, gm;
  int x1, y1, x2, y2, t, deg, b1, b2;
  initgraph(&gd,&gm,"c:\\tc\\bgi");
  printf("Enter coordinates of line: ");
  scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
  printf("Enter angle of rotation: ");
  scanf("%d",&deg);
  line(x1, y1, x2, y2);
  getch();
  t = (22*deg)/(180*7);
  b1 = cos(t)*x1 - sin(t)*y1;
  b2 = cos(t)*x1 + sin(t)*y1;
  line(x1,y1,b1,b2);
  getch();
  closegraph();
}

问题在于它会生成一些静态输出,并且不会根据给定的输入旋转线条。对于任何值 deg 变量,旋转线几乎相似。

输出:

【问题讨论】:

  • 使用浮点数。如果你有一个不错的编译器,你会收到很多关于隐式整数到浮点转换的警告。你不应该通过非标准的、古老的垃圾编译器来学习 C 编程。

标签: c graphics turbo-c


【解决方案1】:

您的变量tint,但三角函数需要floats 或doubles。

所以如果你声明:

int x1, y1, x2, y2, deg, b1, b2;
float t;

它应该工作。 您的程序中可能还有更多问题。

顺便说一句:给你的变量一些明显的名字,例如angle 而不是 t.

此外,从度数到弧度的转换有点笨拙,因为 22/7 是 PI 的一个相当粗略的近似值:

t = (22*deg)/(180*7);

改用这个:

t = 3.1415926 * deg / 180

甚至只是这个(如果在你的 math.h 包含文件中声明了 PI)

t = PI * deg / 180

【讨论】:

  • 我做了以上修改还是没有帮助!
  • 它应该可以工作。通过插入一些 printfs 来调试您的程序,以查看您在变量中获得的值(例如,printf("x1 = %d, x2 = %d\n", x1, x2);)。
  • 我试过上面的方法,它给出了一个静态值 b1 为 3702 和 b2 为 3208
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-11-08
  • 1970-01-01
  • 2017-04-22
  • 2016-06-25
  • 1970-01-01
  • 1970-01-01
  • 2013-10-04
相关资源
最近更新 更多