Now I made my inside-checking function faster, but I now get some error (WA) because of precision (i think) and something connected to it.
my codes:
Code: Select all
program project2;
{$APPTYPE CONSOLE}
{$R-}{$S-}{$Q-}
var
xx1,yy1,xx2,yy2,xx3,yy3,eps,s: extended;
x,y,ans: longint;
xxx,xxxm,yyy,yyym: longint;
function d(x1,y1,x2,y2: extended): extended;
begin
result := sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
end;
function f(x1,y1,x2,y2,x3,y3: extended): extended;
begin
result := abs((x2-x1)*(y3-y1)-(x3-x1)*(y2-y1));
end;
function inside (x,y: extended): boolean;
var
s1,s2,s3: extended;
begin
s1 := f(x,y,xx1,yy1,xx2,yy2);
s2 := f(x,y,xx2,yy2,xx3,yy3);
s3 := f(x,y,xx1,yy1,xx3,yy3);
result := (abs(s1+s2+s3-s) <= eps);
end;
function min (a,b: extended): extended;
begin
result := a;
if b<a then result := b
end;
function max (a,b: extended): extended;
begin
result := a;
if b>a then result := b
end;
function min3(a,b,c: extended): extended;
begin
result := min(a,min(b,c));
end;
function max3(a,b,c: extended): extended;
begin
result := max(a,max(b,c));
end;
begin
eps := 0.0000001;
readln (xx1,yy1,xx2,yy2,xx3,yy3);
while (not((xx1=0)and(xx2=0)and(yy1=0)and(yy2=0)and(xx3=0)and(yy3=0))) do
begin
s := f(xx1,yy1,xx2,yy2,xx3,yy3);
ans := 0;
xxx := round(min3(xx1,xx2,xx3));
if (xxx<1) then xxx:=1;
xxxm :=round(max3(xx1,xx2,xx3));
if (xxxm>99) then xxxm:=99;
yyy := round(min3(yy1,yy2,yy3));
if (yyy<1) then yyy:=1;
yyym:=round(max3(yy1,yy2,yy3));
if (yyym>99) then yyym:=99;
for x:=xxx to xxxm do
for y := yyy to yyym do
if (inside (x,y)) then ans:=ans+1;
writeln (ans:4);
readln (xx1,yy1,xx2,yy2,xx3,yy3);
end;
end.
same in cpp
Code: Select all
#include <cstdlib>
#include <iostream>
#include <cstdio>
#include <math.h>
using namespace std;
long double xx1,yy1,xx2,yy2,xx3,yy3,eps=0.000001,s;
long double abs2(long double n) {
if (n<0)
return -n;
else
return n;
}
long double d(long double x1,long double y1,long double x2, long double y2) {
return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
long double f(long double x1, long double y1, long double x2, long double y2, long double x3, long double y3) {
/* long double a = d(x1,y1,x2,y2), b = d(x1,y1,x3,y3), c = d(x2,y2,x3,y3),p=(a+b+c)/2;
return sqrt(p*(p-a)*(p-b)*(p-c));*/
return abs2((x2-x1)*(y3-y1)-(x3-x1)*(y2-y1));
}
bool inside (int x, int y) {
long double
s1 = f(x,y,xx1,yy1,xx2,yy2),
s2 = f(x,y,xx2,yy2,xx3,yy3),
s3 = f(x,y,xx1,yy1,xx3,yy3);
return (abs2(s1+s2+s3-s) <= eps);
}
long double min (long double a,long double b) {
return a<b?a:b;
}
long double max(long double a,long double b) {
return a>b?a:b;
}
long double min3 (long double a, long double b, long double c) {
return min(a,min(b,c));
}
long double max3 (long double a, long double b, long double c) {
return max(a,max(b,c));
}
int main(int argc, char *argv[]) {
long int x,y,ans;
cin >> xx1 >> yy1 >> xx2 >> yy2 >> xx3 >> yy3;
while (!((xx1==0)&&(xx2==0)&&(yy1==0)&&(yy2==0)&&(xx3==0)&&(yy3==0))) {
s = f(xx1,yy1,xx2,yy2,xx3,yy3);
ans = 0;
int xxx=int (floor(min3(xx1,xx2,xx3)));
if (xxx<1) xxx=1;
int xxxm=int (ceil(max3(xx1,xx2,xx3)));
if (xxxm>99) xxxm=99;
int yyy=int(floor(min3(yy1,yy2,yy3)));
if (yyy<1) yyy=1;
int yyym=int(ceil(max3(yy1,yy2,yy3)));
if (yyym>99) yyym=99;
for (x=xxx;x<=xxxm;x++)
for (y=yyy;y<=yyym;y++)
if (inside (x,y)) ans++;
printf ("%4d\n",ans);
cin >> xx1 >> yy1 >> xx2 >> yy2 >> xx3 >> yy3;
}
return EXIT_SUCCESS;
}