You are here:

C/Computer graphics

Advertisement


Question
Below given is the code for rotating a triangle...and on running it,Floating point error: Domain ,this error occur.Plz help..very urgent.Hope to receive an answer at the earliest.

#include<stdio.h>
#include<graphics.h>
#include<conio.h>
void dda(float x1,float y1,float x2,float y2)
{
float xinc,yinc;
float dx,dy,steps,x,y,k;

dx=x2-x1;
dy=y2-y1;
if(dx>dy)
   steps=dx;
else
   steps=dy;
 xinc=dx/steps;
 yinc=dy/steps;
 x=x1;
 y=y1;
 putpixel(x,y,RED);
for(k=0;k<steps;k++)
{
 x=x+xinc;
 y=y+yinc;
 putpixel(x,y,RED);
}
}

void main()
{
float x2,y2,a,b,c,d,l,x1,y1,l1,x3,y3,rx2,rx3,ry2,ry3;
int gdriver=DETECT,gmode,errorcode;
initgraph(&gdriver,&gmode,"D:\\TC\\BGI");
errorcode=graphresult();
if(errorcode!=grOk)
{
 printf("Graphics error: %s\n",grapherrormsg(errorcode));
 printf("press any key to halt");
 exit(1);
}
printf("enter the vertices: ");
printf("x1,y1: ");
scanf("%f%f",&x1,&y1);
printf("x2,y2: ");
scanf("%f%f",&x2,&y2);
printf("x3,y3: ");
scanf("%f%f",&x3,&y3);
printf("enter the rotation angle: ");
scanf("%f",&a);
b=(3.14/180)*a;
dda(x1,y1,x2,y2);
dda(x1,y1,x3,y3);
dda(x3,y3,x2,y2);
rx2=x1+(x2-x1)*cos(b)-(y2-y1)*sin(b);
ry2=y1+(x2-x1)*sin(b)+(y2-y1)*cos(b);
rx3=x1+(x3-x1)*cos(b)-(y3-y1)*sin(b);
ry3=y1+(x3-x1)*sin(b)+(y3-y1)*cos(b);
dda(x1,y1,rx2,ry2);
 dda(x1,y1,rx3,ry3);
dda(rx3,ry3,rx2,ry2);


getch();
}

Answer
Hi neena,
This error due to you compare float variable in if statement.

#include<stdio.h>
#include<graphics.h>
#include<conio.h>
void dda(int x1,int y1,int x2,int y2)
{
float xinc,yinc,x,y;
int dx,dy,steps,k;
dx=x2-x1;
dy=y2-y1;
if(dx>dy)
  steps=dx;
else
  steps=dy;
xinc=dx/steps;
yinc=dy/steps;
x=x1;
y=y1;
putpixel(x,y,RED);
for(k=0;k<steps;k++)
{
x=x+xinc;
y=y+yinc;
putpixel(x,y,RED);
}
}
void main()
{
int x2,y2,a,b,c,d,l,x1,y1,l1,x3,y3,rx2,rx3,ry2,ry3;
int gdriver=DETECT,gmode,errorcode;
initgraph(&gdriver,&gmode,"D:\\TC\\BGI");
errorcode=graphresult();
if(errorcode!=grOk)
{
printf("Graphics error: %s\n",grapherrormsg(errorcode));
printf("press any key to halt");
exit(1);
}
printf("enter the vertices: ");
printf("x1,y1: ");
scanf("%d%d",&x1,&y1);
printf("x2,y2: ");
scanf("%d%d",&x2,&y2);
printf("x3,y3: ");
scanf("%d%d",&x3,&y3);
printf("enter the rotation angle: ");
scanf("%d",&a);
b=(3.14/180)*a;
dda(x1,y1,x2,y2);
dda(x1,y1,x3,y3);
dda(x3,y3,x2,y2);
rx2=x1+(x2-x1)*cos(b)-(y2-y1)*sin(b);
ry2=y1+(x2-x1)*sin(b)+(y2-y1)*cos(b);
rx3=x1+(x3-x1)*cos(b)-(y3-y1)*sin(b);
ry3=y1+(x3-x1)*sin(b)+(y3-y1)*cos(b);
dda(x1,y1,rx2,ry2);
dda(x1,y1,rx3,ry3);
dda(rx3,ry3,rx2,ry2);
getch();
}

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


bharat kumar dhaker

Expertise

programming question in c.

Experience

programming since 2008 from my college day's.

Education/Credentials
b.tech in computer science.

©2016 About.com. All rights reserved.