J2ME裁减多边形实现
import javax.microedition.lcdui.*;
import java.util.*;
import javax.microedition.rms.*;
import java.io.*;
//import com.nokia.mid.ui.*;
class MainPit extends Canvas implements Runnable
{
Graphics gb;
Image bufImg;
private int nWidth = getWidth();
private int nHeight = getHeight();
public MainPit()
{
try{
bufImg = Image.createImage(nWidth,nHeight);
}catch(Exception e){System.out.println(e+" createImage");}
gb = bufImg.getGraphics();
}
public void paint(Graphics g)
{
g.drawImage(bufImg,0,0,0);
}
int nodes[][] = {
{20,100},
{100,40},
{170,80},
{150,110},
{170,170},
{160,150},
{110,110},
{50,130}
};
/*
* 检查线段x0,y0,x1,y1,与 选段y 的相交情况。
*/
private boolean checkForCut(int y, int y0, int y1)
{
if(y0>=y&&y1<=y){
return true;
}
if(y0<=y&&y1>=y){
return true;
}
return false;
}
// 先进后出堆桟
private int heaps[] = null;
// 入栈
private void push(int intNum)
{
// 增加栈容量
int heapslength;// = heaps.length;
if(heaps != null){
heapslength = heaps.length;
int temp[] = new int[heapslength];
for(int i = 0; i < heapslength; i++){
temp = heaps;
}
heaps = new int[heapslength+1];
for(int i = 0; i < heapslength; i++){
heaps =temp;
}
}else{
heapslength = 0;
heaps = new int[1];
}
// 入栈
heaps[heapslength] = intNum;
System.gc();
}
// 出栈
private int pop()
{
if(heaps==null){return -1;}
int heapslength = heaps.length - 1;
int intNum = heaps[heapslength];
if(heapslength == 0){
heaps = null;
}else{
// 出栈后缩小栈容量
int temp[] = new int[heapslength];
for(int i = 0; i < heapslength; i++)
{
temp = heaps;
}
heaps = new int[heapslength];
for(int i = 0; i < heapslength; i++)
{
heaps = temp;
}
System.gc();
}
return intNum;
}
// 排序栈(从大到小),算法好坏会对扫描算法有直接影响
private void sortHeaps()
{
if(heaps == null){
return;
}
int heapslength = heaps.length;
// 偷懒,偷懒,我就用冒泡排序了^-^&
for(int i = 0; i < heapslength-1; i++){
for(int j = 0; j < heapslength - i - 1; j++){
if(heaps[j]<heaps[j+1]){
int temp = heaps[j];
heaps[j] = heaps[j+1];
heaps[j+1] = temp;
}
}
}
}
private void getCut(int nodes[][],int y)// 将交点坐标x入栈
{
int sideNum = nodes.length;
int tx = 0;
for(int i = 0; i < sideNum-1; i++)
{
int x0 = nodes[0];
int y0 = nodes[1];
int x1 = nodes[i+1][0];
int y1 = nodes[i+1][1];
if(y0 == y1&&y0 == y){
push(x0);
push(x1);
continue;
}
if(checkForCut(y,nodes[1],nodes[i+1][1])){
tx = (x1 - x0) * (y - y0) / (y1 - y0) + x0;
push(tx);
}
}
if(nodes[0][1] == nodes[sideNum-1][1]&&nodes[0][1]==y){
push(nodes[0][0]);
push(nodes[sideNum-1][0]);
}else{
if(checkForCut(y,nodes[sideNum-1][1],nodes[0][1])){
tx = (nodes[0][0] - nodes[sideNum-1][0])*(y-nodes[sideNum-1][1])/(nodes[0][1] - nodes[sideNum-1][1])+nodes[sideNum-1][0];
push(tx);
}
}
sortHeaps();
}
private void fillPolygon(int nodes[][])
{
gb.setColor(0x00ff0000);
for(int i = 0; i < nHeight; i++){
getCut(nodes,i);
if(i == 100)
{
for(int j = 0;j < heaps.length; j++){
System.out.println(" "+heaps[j]);
}
}
int bx = 0;
if(heaps == null)
continue;
int j = 0;
while(heaps != null){
if((j%2)==0){
int ex = pop();
bx = ex;
}else{
int ex = pop();
gb.drawLine(bx,i,ex,i);
bx = ex;
}
j++;
}
}
}
private void drawPolygon(int nodes[][])
{
gb.setColor(0x0000ff00);
for(int i = 0; i < nodes.length-1; i++){
gb.drawLine(nodes[0],nodes[1],nodes[i+1][0],nodes[i+1][1]);
// gb.fillArc(nodes[0] - 10,nodes[1] - 10,20,20,0,360);
}
gb.drawLine(nodes[0][0],nodes[0][1], nodes[nodes.length-1][0], nodes[nodes.length-1][1]);
// gb.fillArc(nodes[nodes.length-1][0] - 10,nodes[nodes.length-1][1] -
// 10,20,20,0,360);
}
// 裁减,裁减后的图放在theCutImg里面
Image theCutImg;
private void cutGraphics(int x,int y,int w, int h)
{
Graphics tg;
try{
theCutImg = Image.createImage(w,h);
}catch(Exception e){System.out.println(e+" createImage");}
tg = theCutImg.getGraphics();
tg.drawImage(bufImg, -x, -y, 0);
}
public void run()
{
gb.setColor(0x00ffffff);
gb.fillRect(0,0,nWidth,nHeight);
fillPolygon(nodes);
System.out.println("done1");
drawPolygon(nodes);
System.out.println("done2");
repaint();
}
private void pause(long t)
{
try{Thread.sleep(t);}catch(Exception e){}
}
public void keyPressed(int keyCode)
{
}
}