猿问

图像/图形成形状

我想知道是否有任何方法可以将图像/图形转换为Shape?例如,我可以将摩托车形状的轮廓转换为,Shape然后在Java中使用它吗?我知道您可以用普通正方形或圆角,多边形等来做。但是有没有办法做自定义形状?



喵喵时光机
浏览 426回答 3
3回答

aluckdog

这是更快但不太准确的东西,对于碰撞检查或2D物理很有用。&nbsp; &nbsp; Point[] MakePoly(BufferedImage spr,int d,int angle){//creates an outline of a transparent image, points are stored in an array//arg0 - BufferedImage source image&nbsp;//arg1 - Int detail (lower = better)//arg2 - Int angle threshold in degrees (will remove points with angle differences below this level; 15 is a good value)//&nbsp; &nbsp; &nbsp; making this larger will make the body faster but less accurate;&nbsp; &nbsp; int w= spr.getWidth(null);&nbsp; int h= spr.getHeight(null);&nbsp; &nbsp; // increase array size from 255 if needed&nbsp; &nbsp; int[] vertex_x=new int[255], vertex_y=new int[255], vertex_k=new int[255];&nbsp;&nbsp; &nbsp; int numPoints=0, tx=0,ty=0,fy=-1,lx=0,ly=0; vertex_x[0]=0; vertex_y[0]=0; vertex_k[0]=1;&nbsp;&nbsp; &nbsp; for (tx=0;tx<w;tx+=d)&nbsp; for (ty=0;ty<h;ty+=1)&nbsp; &nbsp; &nbsp; &nbsp;if((spr.getRGB(tx,ty)>>24) != 0x00 )&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {vertex_x[numPoints]=tx; vertex_y[numPoints]=h-ty; vertex_k[numPoints]=1; numPoints++; if (fy<0) fy=ty; lx=tx; ly=ty; break;&nbsp; }&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; for (ty=0;ty<h;ty+=d)&nbsp; for (tx=w-1;tx>=0;tx-=1)&nbsp; &nbsp; if((spr.getRGB(tx,ty)>>24)&nbsp; != 0x00 && ty > ly)&nbsp; &nbsp; &nbsp; &nbsp; {vertex_x[numPoints]=tx; vertex_y[numPoints]=h-ty; vertex_k[numPoints]=1; numPoints++; lx=tx; ly=ty; break;&nbsp; }&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; for (tx=w-1;tx>=0;tx-=d)&nbsp; for (ty=h-1;ty>=0;ty-=1) if((spr.getRGB(tx,ty)>>24) != 0x00 && tx < lx)&nbsp; &nbsp; &nbsp; &nbsp; {vertex_x[numPoints]=tx; vertex_y[numPoints]=h-ty; vertex_k[numPoints]=1; numPoints ++; lx=tx; ly=ty; break; }&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; for (ty=h-1;ty>=0;ty-=d)&nbsp; for (tx=0;tx<w;tx+=1)&nbsp; &nbsp; if((spr.getRGB(tx,ty)>>24) != 0x00 && ty < ly && ty > fy)&nbsp; &nbsp; &nbsp; &nbsp; {vertex_x[numPoints]=tx; vertex_y[numPoints]=h-ty; vertex_k[numPoints]=1; numPoints ++; lx=tx; ly=ty; break; }&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; double ang1,ang2;&nbsp; &nbsp; &nbsp; &nbsp;for (int i=0;i<numPoints-2;i++) {&nbsp; &nbsp; &nbsp; &nbsp; ang1 = PointDirection(vertex_x[i],vertex_y[i], vertex_x[i+1],vertex_y[i+1]);&nbsp; &nbsp; &nbsp; &nbsp; ang2 = PointDirection(vertex_x[i+1],vertex_y[i+1], vertex_x[i+2],vertex_y[i+2]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (Math.abs(ang1-ang2) <= angle)&nbsp; &nbsp;vertex_k[i+1] = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; ang1 = PointDirection(vertex_x[numPoints-2],vertex_y[numPoints-2], vertex_x[numPoints-1],vertex_y[numPoints-1]);&nbsp; &nbsp; ang2 = PointDirection(vertex_x[numPoints-1],vertex_y[numPoints-1], vertex_x[0],vertex_y[0]);&nbsp; &nbsp; &nbsp;if (Math.abs(ang1-ang2) <= angle)&nbsp; &nbsp; &nbsp; vertex_k[numPoints-1] = 0;&nbsp;&nbsp; &nbsp; ang1 = PointDirection(vertex_x[numPoints-1],vertex_y[numPoints-1], vertex_x[0],vertex_y[0]);&nbsp; &nbsp; ang2 = PointDirection(vertex_x[0],vertex_y[0], vertex_x[1],vertex_y[1]);&nbsp; &nbsp; &nbsp;if (Math.abs(ang1-ang2) <= angle)&nbsp; &nbsp; &nbsp; vertex_k[0] = 0;&nbsp; &nbsp; &nbsp;int n=0;for (int i=0;i<numPoints;i++)if(vertex_k[i]==1)n++;&nbsp; &nbsp; Point[] poly= new Point[n]; n=0; for (int i=0;i<numPoints;i++) if (vertex_k[i]==1)&nbsp; &nbsp; { poly[n]=new Point(); poly[n].x=vertex_x[i]; poly[n].y=h-vertex_y[i];n++;} return poly;}double PointDirection(double xfrom,double yfrom,double xto,double yto){&nbsp; &nbsp; return&nbsp; Math.atan2(yto-yfrom,xto-xfrom)*180/Math.PI ;}
随时随地看视频慕课网APP

相关分类

Java
我要回答