WAP之家:为您提供最全最新的WAP技术,CP.SP.3G等行业资讯。 WAP之家交流论坛全新开放 点击进入>>
WAP资讯 | 3G动态 | SP动态 | 运营商动态 | 内容商动态 | 制造商动态 | 论坛讨论>> 每次自动访问
WAP技术 | WAP源码 | 手机编程 | 手机源码 | 无线技术 | J2ME技术 | 手机软件 添加到收藏夹
IVR技术 | SP资料 | SMS MMS技术 | 商业方案 | IVR下载 | 书籍教程 | 工具软件 语言:繁體中文

WAP之家技术文章J2ME技术程序开发任意多边形的填充算法(扫描线发式

任意多边形的填充算法(扫描线发式
作者:不详  来源:本站整理  发布时间:2005-11-24 16:48:28

import java.lang.*;
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[][] = {//8/
{20,100},//0
{100,40},//1
{170,80},//2
{150,110},//3
{170,170},//4
{160,150},//5
{110,110},//6
{50,130}//7
};

/*
检查线段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[i] = heaps[i];
}
heaps = new int[heapslength+1];
for(int i = 0; i < heapslength; i++){
heaps[i] =temp[i];
}
}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[i] = heaps[i];
}
heaps = new int[heapslength];
for(int i = 0; i < heapslength; i++)
{
heaps[i] = temp[i];
}
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[i][0];
int y0 = nodes[i][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[i][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[0][1],nodes[sideNum-1][1])){
tx = (nodes[0]

[1] [2]  下一页

[] [返回上一页] [打 印]
文章评论

用户名: 查看更多评论

分 值:100分 85分 70分 55分 40分 25分 10分 0分

内 容:

         (注“”为必填内容。) 验证码: 验证码,看不清楚?请点击刷新验证码