So I’m making an attempt to attract and fill a triangle on the display screen utilizing the barycentric methodology, however my barycentric implementation appears to be outputing outdoors triangle coordinates, why?
On-line C++ Compiler.
Code, Compile, Run and Debug C++ program on-line.
Write your code on this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#embrace <iostream>
#embrace <cmath>
utilizing namespace std;
double triarea(double a, double b, double c)
{
double s = (a + b + c)/2.0;
double space=sqrt(fabs(s*(s-a)*(s-b)*(s-c)));
return space;
}
// compute the space between two second factors
double dist(double x0, double y0, double x1, double y1)
{
double a = x1 - x0;
double b = y1 - y0;
return sqrt(a*a + b*b);
}
double returnMax(double A, double B, double C)
{
return (A > B ? A : B) > C ? (A > B ? A : B) : C;
}
double returnMin(double A, double B, double C)
{
return (A > B ? B : A) > C ? C : (A > B ? B : A);
}
double barycentric(double x0, double y0, double x1, double y1, double x2, double y2, double px, double py)
{
double a = dist(x0, y0, x1, y1);
double b = dist(x1, y1, x2, y2);
double c = dist(x2, y2, x0, y0);
double ABC= triarea(a,b,c);
double pa = dist(x0, y0, px, py);
double pb = dist(px, py, x2, y2);
double pc = dist(x2, y2, x0, y0);
double PAB= triarea(pa,pb,pc);
double paa = dist(x1, y1, px, py);
double pbb = dist(px, py, x2, y2);
double pcc = dist(x2, y2, x1, y1);
double PBA= triarea(paa,pbb,pcc);
double inorout = 1 - (PAB/(double)ABC) - (PBA/(double)ABC);
return inorout;
}
void drawtriangle(double x0, double y0, double x1, double y1, double x2, double y2)
{
double maxx = returnMax(x0,x1,x2);
double minx = returnMin(x0,x1,x2);
double maxy = returnMax(y0,y1,y2);
double miny = returnMin(y0,y1,y2);
for(double x = minx; x < maxx; x++)
{
for(double y = miny; y < maxy; y++)
{
if(barycentric(x0,y0,x1,y1,x2,y2,x,y) >=0)
{
//draw pixel
cout<< "(" << x <<","<< y<<")" <<endl;
}
}
}
}
int fundamental()
{
drawtriangle(-5,-5,10,30,20,5);
return 0;
}