user3382203
user3382203

Reputation: 179

C graphics program generating static output

I have written a simple code to rotate a line. Following is the source code:

#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();
}

The issue is that it generates a somewhat static output and does not rotate the line according to the input given. The rotated line is almost similar for any value deg variable.

Output: enter image description here enter image description here

Upvotes: 1

Views: 192

Answers (1)

Jabberwocky
Jabberwocky

Reputation: 50778

Your variable t is an int, but the trigonometric functions require floats or doubles.

So if you declare:

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

It should work. There may be more issues in your program.

BTW: give some obvious names to your variables, e.g. angle instead of t.

Also your conversion from degrees to radian is a bit clumsy as 22/7 is a rather crude approximation of PI:

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

use rather this:

t = 3.1415926 * deg / 180

or even just this (if PI is declared in your math.h include file)

t = PI * deg / 180

Upvotes: 1

Related Questions