已经尽力了。继续深入压缩代码的方法肯定很诡异了。
参考了基于高级一些的Mathematica下的办法:
list=Table[Manipulate[With[{p=Rest@pts,pt=First@pts},Graphics[{If[testpoint[p,pt],Pink,Cyan],Polygon@p},PlotRange->3 {{-1,1},{-1,1}},ImageSize->{380,415},PlotLabel->Text[Style[If[testpoint[p,pt],"在其中","已出局"],FontFamily->"Arial",If[testpoint[p,pt],Red,Blue],30]]]],{{pts,{{0,0},{x,1/2},{-1,-1},{2,-3/2},{1,2},{0,1}}},Sequence@@(3 {{-1,-1},{1,1}}),Locator,LocatorAutoCreate->{4,Infinity}},SaveDefinitions->True,Initialization:>((*test if point pt inside polygon poly*)testpoint[poly_,pt_]:=Round[(Total@Mod[(#-RotateRight[#])&@(ArcTan@@(pt-#)&/@poly),2 Pi,-Pi]/2/Pi)]!=0)],{x,-2,2.55,.1}];
Export["testWinding.gif",list]
输出这样的图片为了好看:
可能达到的最短C++代码(再短小于500字符基于当前方法也难)
// NOTE: compile with g++ filename.cpp -std=c++11
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <vector>
//#include <functional>
#define DIM1 600
#define DIM2 400
//#define DM1 (DIM1-1)
//#define DM2 (DIM2-1)
//#define _sq(x) ((x)*(x)) // square
//#define _cb(x) abs((x)*(x)*(x)) // absolute value of cube
//#define _cr(x) (unsigned char)(pow((x),1.0/3.0)) // cube root
unsigned char GR(int,int);
unsigned char BL(int,int);
#define F float
F e=1.e-7,p=3.14;
unsigned char RD(int i,int j){
// YOUR CODE HERE
#define E for(int k=0;k<5;k++){
#define at(x,y) abs(x)>e?x>e?atan(y/x):y>0?atan(y/x)+p:atan(y/x)-p:y>0?p/2:-p/2
return 255;
}
bool Q(int i,int j,int* x){
F t[5],o;
E
t[k]=at((F)(x[k]-i),(F)(x[k+5]-j));
}
F tl=0.;
E
o=t[(k+1)%5]-t[k];
o+=o<-p?p:o>p?-p:0;
tl+=o;
}
return abs(tl)>.5;
};
unsigned char GR(int i,int j){
// YOUR CODE HERE
int q[10]={100, 135, 43, 157, 65, 40, 149, 81, 81, 149},
r[10]={184, 220, 183, 207, 205, 168, 181, 191, 161, 199},
s[10]={183, 208, 204, 185, 220, 50, 22, 59, 27, 42},
t[10]={221, 259, 229, 239, 252, 135, 133, 157, 120, 156},
u[10]={220, 254, 237, 231, 258,83, 66, 100, 62, 89};
return Q(i,j,q)||Q(i,j,r)||Q(i,j,s)||Q(i,j,t)||Q(i,j,u)?255:0;
}
unsigned char BL(int i,int j){
// YOUR CODE HERE
return 0;
}
void pixel_write(int,int);
FILE *fp;
int main(){
fp = fopen("MathPic.ppm","wb");
fprintf(fp, "P6\n%d %d\n255\n", DIM1, DIM2);
for(int j=0;j<DIM2;j++)
for(int i=0;i<DIM1;i++)
pixel_write(i,j);
fclose(fp);
system("pause");
return 0;
}
void pixel_write(int i, int j){
static unsigned char color[3];
color[0] = RD(i,j)&255;
color[1] = GR(i,j)&255;
color[2] = BL(i,j)&255;
fwrite(color, 1, 3, fp);
}
输出虽然近似,也是正常的红旗:
原文地址:http://blog.csdn.net/stereohomology/article/details/46463397