|
|
@ -5,7 +5,6 @@ import java.awt.Toolkit; |
|
|
|
import java.awt.image.ColorModel; |
|
|
|
import java.awt.image.ColorModel; |
|
|
|
import java.awt.image.MemoryImageSource; |
|
|
|
import java.awt.image.MemoryImageSource; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.HashMap; |
|
|
|
|
|
|
|
import java.util.List; |
|
|
|
import java.util.List; |
|
|
|
import java.awt.event.KeyEvent; |
|
|
|
import java.awt.event.KeyEvent; |
|
|
|
import java.awt.event.MouseEvent; |
|
|
|
import java.awt.event.MouseEvent; |
|
|
@ -268,138 +267,46 @@ public class Panel extends JPanel implements Runnable,KeyListener { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public void FillCircle(double center_x,double center_y,double r,Color col) { |
|
|
|
public void FillCircle(double center_x,double center_y,double r,Color col) { |
|
|
|
int counter=0; |
|
|
|
List<Point<Double>> points = new ArrayList<Point<Double>>(); |
|
|
|
List<Point<Integer>> points = new ArrayList<Point<Integer>>(); |
|
|
|
points.add(new Point<Double>(center_x,center_y)); |
|
|
|
for (double theta=0;theta<Math.PI*2;theta+=((Math.PI*2)/CIRCLE_PRECISION)) { |
|
|
|
for (double theta=0;theta<Math.PI*2;theta+=((Math.PI*2)/CIRCLE_PRECISION)) { |
|
|
|
//System.out.println("Loop "+counter+++". Theta:"+theta);
|
|
|
|
//System.out.println("Loop "+counter+++". Theta:"+theta);
|
|
|
|
//System.out.println("X:"+(Math.sin(theta)*r+center_x)+" Y:"+(Math.cos(theta)*r+center_y));
|
|
|
|
//System.out.println("X:"+(Math.sin(theta)*r+center_x)+" Y:"+(Math.cos(theta)*r+center_y));
|
|
|
|
points.add(new Point<Integer>((int)(Math.round(Math.sin(theta)*r+center_x)),(int)(Math.round(Math.cos(theta)*r+center_y)))); |
|
|
|
points.add(new Point<Double>((double)(Math.round(Math.sin(theta)*r+center_x)),(double)(Math.round(Math.cos(theta)*r+center_y)))); |
|
|
|
} |
|
|
|
} |
|
|
|
FillPolygon(0,0,col,points); |
|
|
|
FillPolygon(0d,0d,col,points,PolygonStructure.FAN); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void FillOval(double center_x,double center_y,double w,double h,Color col) { |
|
|
|
public void FillOval(double center_x,double center_y,double w,double h,Color col) { |
|
|
|
int counter=0; |
|
|
|
List<Point<Double>> points = new ArrayList<Point<Double>>(); |
|
|
|
List<Point<Integer>> points = new ArrayList<Point<Integer>>(); |
|
|
|
|
|
|
|
double r = Math.max(w,h); |
|
|
|
double r = Math.max(w,h); |
|
|
|
double ratio = Math.min(w,h)/r; |
|
|
|
double ratio = Math.min(w,h)/r; |
|
|
|
|
|
|
|
points.add(new Point<Double>(center_x,center_y)); |
|
|
|
for (double theta=0;theta<Math.PI*2;theta+=((Math.PI*2)/CIRCLE_PRECISION)) { |
|
|
|
for (double theta=0;theta<Math.PI*2;theta+=((Math.PI*2)/CIRCLE_PRECISION)) { |
|
|
|
//System.out.println("Loop "+counter+++". Theta:"+theta);
|
|
|
|
Point<Double> newP = new Point<Double>((double)(Math.round(Math.sin(theta)*r)),(double)(Math.round(Math.cos(theta)*r))); |
|
|
|
//System.out.println("X:"+(Math.sin(theta)*r+center_x)+" Y:"+(Math.cos(theta)*r+center_y));
|
|
|
|
|
|
|
|
Point<Integer> newP = new Point<Integer>((int)(Math.round(Math.sin(theta)*r)),(int)(Math.round(Math.cos(theta)*r))); |
|
|
|
|
|
|
|
if (w<h) { |
|
|
|
if (w<h) { |
|
|
|
newP.x=(int)Math.round(newP.x*ratio); |
|
|
|
newP.x=(double)Math.round(newP.x*ratio); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
newP.y=(int)Math.round(newP.y*ratio); |
|
|
|
newP.y=(double)Math.round(newP.y*ratio); |
|
|
|
} |
|
|
|
} |
|
|
|
newP.x+=(int)center_x; |
|
|
|
newP.x+=center_x; |
|
|
|
newP.y+=(int)center_y; |
|
|
|
newP.y+=center_y; |
|
|
|
points.add(newP); |
|
|
|
points.add(newP); |
|
|
|
} |
|
|
|
} |
|
|
|
FillPolygon(0,0,col,points); |
|
|
|
FillPolygon(0d,0d,col,points,PolygonStructure.FAN); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public void FillPolygon(double x_offset,double y_offset,Color col,List<Point<Integer>>points) { |
|
|
|
public void FillPolygon(double x_offset,double y_offset,Color col,List<Point<Double>>points,PolygonStructure structure) { |
|
|
|
Edge[] edges = new Edge[points.size()]; |
|
|
|
List<Point<Double>> tex = new ArrayList<>(); |
|
|
|
List<Edge> edges_sorted = new ArrayList<Edge>(); |
|
|
|
List<Color> cols = new ArrayList<>(); |
|
|
|
for (int i=0;i<points.size();i++) { |
|
|
|
for (int i=0;i<points.size();i++) { |
|
|
|
edges[i] = new Edge(points.get(i),points.get((i+1)%points.size())); |
|
|
|
tex.add(new Point<Double>((double)0,(double)0)); |
|
|
|
if (!Double.isInfinite(edges[i].inverse_slope)) { |
|
|
|
cols.add(col); |
|
|
|
if (edges_sorted.size()==0) { |
|
|
|
} |
|
|
|
edges_sorted.add(edges[i]); |
|
|
|
FillTexturedPolygon(points,tex,cols,null,structure); |
|
|
|
} else { |
|
|
|
|
|
|
|
boolean inserted=false; |
|
|
|
|
|
|
|
for (int j=0;j<edges_sorted.size();j++) { |
|
|
|
|
|
|
|
Edge e2 = edges_sorted.get(j); |
|
|
|
|
|
|
|
if (e2.min_y>=edges[i].min_y) { |
|
|
|
|
|
|
|
edges_sorted.add(j,edges[i]); |
|
|
|
|
|
|
|
inserted=true; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (!inserted) { |
|
|
|
|
|
|
|
edges_sorted.add(edges[i]); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
//System.out.println(edges_sorted);
|
|
|
|
|
|
|
|
List<Edge> active_edges = new ArrayList<Edge>(); |
|
|
|
|
|
|
|
scanLine = edges_sorted.get(0).min_y-1; |
|
|
|
|
|
|
|
nextScanLine = scanLine+1; |
|
|
|
|
|
|
|
do { |
|
|
|
|
|
|
|
for (int i=0;i<active_edges.size();i+=2) { |
|
|
|
|
|
|
|
if (i>=active_edges.size()-1) break; |
|
|
|
|
|
|
|
Edge e1 = active_edges.get(i); |
|
|
|
|
|
|
|
Edge e2 = active_edges.get(i+1); |
|
|
|
|
|
|
|
//System.out.println("Drawing from "+((int)Math.round(e1.x_of_min_y))+" to "+e2.x_of_min_y+" on line "+scanLine);
|
|
|
|
|
|
|
|
for (int x=(int)Math.round(e1.x_of_min_y);x<=e2.x_of_min_y;x++) { |
|
|
|
|
|
|
|
if (x<0||x>JavaProjectTemplate.WINDOW_WIDTH) continue; |
|
|
|
|
|
|
|
int index = (scanLine+(int)y_offset)*getWidth()+x+(int)x_offset; |
|
|
|
|
|
|
|
if (index<pixel.length&&index>=0) { |
|
|
|
|
|
|
|
Draw(index,col.getColor()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
List<Edge> new_active_edges = new ArrayList<Edge>(); |
|
|
|
|
|
|
|
for (int i=0;i<active_edges.size();i++) { |
|
|
|
|
|
|
|
Edge e = active_edges.get(i); |
|
|
|
|
|
|
|
if (e.max_y==scanLine+1) { |
|
|
|
|
|
|
|
active_edges.remove(i--); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
e.x_of_min_y+=e.inverse_slope; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
scanLine++; |
|
|
|
|
|
|
|
for (int i=0;i<active_edges.size();i++) { |
|
|
|
|
|
|
|
Edge e = active_edges.get(i); |
|
|
|
|
|
|
|
boolean inserted=false; |
|
|
|
|
|
|
|
for (int j=0;j<new_active_edges.size();j++) { |
|
|
|
|
|
|
|
Edge e2 = new_active_edges.get(j); |
|
|
|
|
|
|
|
if (e2.x_of_min_y>e.x_of_min_y) { |
|
|
|
|
|
|
|
new_active_edges.add(j,e); |
|
|
|
|
|
|
|
inserted=true; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (!inserted) { |
|
|
|
|
|
|
|
new_active_edges.add(e); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
active_edges=new_active_edges; |
|
|
|
|
|
|
|
GetNextScanLineEdges(edges_sorted, active_edges); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
while (active_edges.size()>0); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void GetNextScanLineEdges(List<Edge> edges_sorted, List<Edge> active_edges) { |
|
|
|
|
|
|
|
if (scanLine==nextScanLine) { |
|
|
|
|
|
|
|
for (int i=0;i<edges_sorted.size();i++) { |
|
|
|
|
|
|
|
Edge e = edges_sorted.get(i); |
|
|
|
|
|
|
|
if (e.min_y==scanLine) { |
|
|
|
|
|
|
|
e = edges_sorted.remove(i--); |
|
|
|
|
|
|
|
boolean inserted=false; |
|
|
|
|
|
|
|
for (int j=0;j<active_edges.size();j++) { |
|
|
|
|
|
|
|
if (e.x_of_min_y<active_edges.get(j).x_of_min_y) { |
|
|
|
|
|
|
|
active_edges.add(j,e); |
|
|
|
|
|
|
|
inserted=true; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (!inserted) { |
|
|
|
|
|
|
|
active_edges.add(e); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} else |
|
|
|
|
|
|
|
if (e.min_y>scanLine) { |
|
|
|
|
|
|
|
nextScanLine=e.min_y; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void Draw(int x, int y, Color col) { |
|
|
|
public void Draw(int x, int y, Color col) { |
|
|
|
if (x<0||y<0||x>=JavaProjectTemplate.WINDOW_WIDTH||y>=JavaProjectTemplate.WINDOW_HEIGHT) return; |
|
|
|
if (x<0||y<0||x>=JavaProjectTemplate.WINDOW_WIDTH||y>=JavaProjectTemplate.WINDOW_HEIGHT) return; |
|
|
|
Draw(y*JavaProjectTemplate.WINDOW_WIDTH+x,col.getColor()); |
|
|
|
Draw(y*JavaProjectTemplate.WINDOW_WIDTH+x,col.getColor()); |
|
|
|