计算机图形学-java实现-多边形填充扫描线算法

import javax.swing.JFrame;

public class MyFill {
 public static void main(String args[]) {
  lineScan();
 }

 private static void lineScan() {
  JFrame frame = new JFrame();
  TwoDimen env = new TwoDimen();
  frame.getContentPane().add(env);
  frame.setBounds(100, 100, 600, 600);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setResizable(false);
  frame.setVisible(true);
  ET et = new ET(15);
  et.insert(3, new Item(5, 7, -5 / 3, null));
  et.insert(3, new Item(4, 12, 2, null));
  et.insert(4, new Item(9, 16, 0, null));
  et.insert(5, new Item(10, 2, 0, null));
  et.insert(6, new Item(10, 9, -7 / 4, null));
  et.insert(6, new Item(9, 9, 7 / 3, null));
  AEL ael = new AEL();
  Item a,b;
  for (int y = 0; y < 15; y++) {
   Item temp = et.delete(y);
   while (temp != null) {
    ael.insert(temp);
    temp = et.delete(y);
   }
   while(ael.delete(y)!=null){
    
   }
   ael.initialIndex();
   a=ael.next();
   b= ael.next();
   System.out.println();
   ael.display();
   while (a != null && b != null) {
    for (int j = a.x; j < b.x; j++) {
     env.drawPoint(j, y);
     System.out.print("("+j+","+y+")");
    }
    a = ael.next();
    b = ael.next();
    System.out.print("空隙");
   }
   ael.enhanceX();
  }
 }

}

class AEL {
 private Item head;
 private Item index;

 public AEL() {
  head = null;
 }

 public void initialIndex() {
  index = head;
 }

 public Item next() {
  if (index == null)
   return null;
  Item temp = index;
  index = index.next;
  return temp;
 }

 public void enhanceX() {
  Item item;
  item = head;
  while (item != null) {
   item.x = (int) (item.x + item.deltaX);
   item = item.next;
  }
 }

 public void insert(Item item) {
  Item temp = head;
  Item prev = null;
  while (temp != null && item.x > temp.x) {
   prev = temp;
   temp = temp.next;
  }
  while (temp != null && item.x == temp.x && item.deltaX > temp.deltaX) {
   prev = temp;
   temp = temp.next;
  }
  if (head == temp) {
   item.next = head;
   head = item;
  } else {
   item.next = temp;
   prev.next = item;
  }
 }

 public Item delete(int yMax) {
  Item temp = head;
  Item prev = null;
  while (temp != null) {
   if (temp.yMax == yMax) {
    if (prev != null) {
     prev.next = temp.next;
    } else {
     head = temp.next;
    }
    break;
   }
   prev=temp;
   temp = temp.next;
  }
  return temp;
 }

 public void display() {
  Item temp = head;
  while (temp != null) {
   System.out.println(temp.yMax + "  " + temp.x + "  " + temp.deltaX);
   temp = temp.next;
  }
 }
}

class ET {
 private int yNum;
 private Item[] et;

 public ET(int yNum) {
  this.yNum = yNum;
  this.et = new Item[yNum + 1];
 }

 public void insert(int y, Item item) {
  item.next = et[y];
  et[y] = item;
 }

 public Item delete(int y) {
  Item temp = null;
  if (et[y] != null) {
   temp = et[y];
   et[y] = et[y].next;
  }
  return temp;
 }

 public void display() {
  Item temp;
  for (int i = 0; i < yNum + 1; i++) {
   temp = et[i];
   System.out.println("第" + i + "列");
   while (temp != null) {
    System.out.println(temp.yMax + "  " + temp.x + "  "
      + temp.deltaX);
    temp = temp.next;
   }
  }
 }
}

class Item {
 public int yMax;
 public int x;
 public float deltaX;
 public Item next;

 public Item(int yMax, int x, float deltaX, Item next) {
  this.yMax = yMax;
  this.x = x;
  this.deltaX = deltaX;
  this.next = next;
 }
}

你可能感兴趣的:(算法)