Fill triangle implementation

Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
gpu
sigonasr2 2 years ago
parent 7b663847ef
commit 16f225c423
  1. 2
      src/sig/JavaProjectTemplate.java
  2. 168
      src/sig/engine/Panel.java

@ -66,7 +66,7 @@ public class JavaProjectTemplate {
game.Draw_Text(10,40,new sig.engine.String("Hello World!"),Font.PROFONT_12);
game.Draw_Text_Ext(10,52,new sig.engine.String("Hello World 2!"),Font.PROFONT_36,Color.MAGENTA);
game.Draw_
game.Fill_Triangle(160,160,190,190,50,250,Color.RED);
}
public static void main(String[] args) {

@ -237,7 +237,7 @@ public class Panel extends JPanel implements Runnable,KeyListener {
gameInstance.drawGame();
}
public void FillCircle(Color col,double center_x,double center_y,double r) {
public void FillCircle(double center_x,double center_y,double r,Color col) {
int counter=0;
Point[] points = new Point[CIRCLE_PRECISION];
for (double theta=0;theta<Math.PI*2;theta+=((Math.PI*2)/CIRCLE_PRECISION)) {
@ -245,11 +245,11 @@ public class Panel extends JPanel implements Runnable,KeyListener {
//System.out.println("X:"+(Math.sin(theta)*r+center_x)+" Y:"+(Math.cos(theta)*r+center_y));
points[counter++]=new Point((int)(Math.round(Math.sin(theta)*r+center_x)),(int)(Math.round(Math.cos(theta)*r+center_y)));
}
FillPolygon(col,0,0,points);
FillPolygon(0,0,col,points);
}
public void FillOval(Color col,double center_x,double center_y,double w,double h) {
public void FillOval(double center_x,double center_y,double w,double h,Color col) {
int counter=0;
Point[] points = new Point[CIRCLE_PRECISION];
double r = Math.max(w,h);
@ -267,10 +267,10 @@ public class Panel extends JPanel implements Runnable,KeyListener {
newP.y+=center_y;
points[counter++]=newP;
}
FillPolygon(col,0,0,points);
FillPolygon(0,0,col,points);
}
public void FillPolygon(Color col,double x_offset,double y_offset,Point...points) {
public void FillPolygon(double x_offset,double y_offset,Color col,Point...points) {
Edge[] edges = new Edge[points.length];
List<Edge> edges_sorted = new ArrayList<Edge>();
for (int i=0;i<points.length;i++) {
@ -516,7 +516,7 @@ public class Panel extends JPanel implements Runnable,KeyListener {
}
}
public void Fill_Rect(Color col,double x,double y,double w,double h) {
public void Fill_Rect(double x,double y,double w,double h,Color col) {
for (int xx=0;xx<w;xx++) {
for (int yy=0;yy<h;yy++) {
if (x+xx>=0&&y+yy>=0&&x+xx<JavaProjectTemplate.WINDOW_WIDTH&&y+yy<JavaProjectTemplate.WINDOW_HEIGHT) {
@ -584,6 +584,162 @@ public class Panel extends JPanel implements Runnable,KeyListener {
}
}
public void Draw_Rect(double x,double y, double w, double h, Color col) {
Draw_Line((int)x, (int)y, (int)x+(int)w, (int)y, col);
Draw_Line((int)x+(int)w, (int)y, (int)x+(int)w, (int)y+(int)h, col);
Draw_Line((int)x+(int)w, (int)y+(int)h, (int)x, (int)y+(int)h, col);
Draw_Line((int)x, (int)y+(int)h, (int)x, (int)y, col);
}
public void Draw_Triangle(double x1, double y1, double x2, double y2, double x3, double y3, Color col) {
Draw_Line((int)x1, (int)y1, (int)x2, (int)y2, col);
Draw_Line((int)x2, (int)y2, (int)x3, (int)y3, col);
Draw_Line((int)x3, (int)y3, (int)x1, (int)y1, col);
}
private void drawline(int sx,int ex,int ny,Color col){
for (int i = sx; i <= ex; i++) Draw(i, ny, col);
}
public void Fill_Triangle(double x1, double y1, double x2, double y2, double x3, double y3, Color col){
int t1x, t2x, y, minx, maxx, t1xp, t2xp;
boolean changed1 = false;
boolean changed2 = false;
int signx1, signx2, dx1, dy1, dx2, dy2;
int e1, e2;
// Sort vertices
int temp;
if (y1 > y2) {temp=(int)y1;y1=y2;y2=temp;temp=(int)x1;x1=x2;x2=temp;}
if (y1 > y3) {temp=(int)y1;y1=y3;y3=temp;temp=(int)x1;x1=x3;x3=temp;}
if (y2 > y3) {temp=(int)y2;y2=y3;y3=temp;temp=(int)x2;x2=x3;x3=temp;}
t1x = t2x = (int)x1; y = (int)y1; // Starting points
dx1 = (int)(x2 - x1);
if (dx1 < 0) { dx1 = -dx1; signx1 = -1; }
else signx1 = 1;
dy1 = (int)(y2 - y1);
dx2 = (int)(x3 - x1);
if (dx2 < 0) { dx2 = -dx2; signx2 = -1; }
else signx2 = 1;
dy2 = (int)(y3 - y1);
if (dy1 > dx1) {temp=dx1;dx1=dy1;dy1=temp;changed1 = true; }
if (dy2 > dx2) {temp=dx2;dx2=dy2;dy2=temp;changed2 = true; }
e2 = (int)(dx2 >> 1);
// Flat top, just process the second half
boolean firstHalfDone=false;
if (y1 == y2) firstHalfDone=true;
if (!firstHalfDone) {
e1 = (int)(dx1 >> 1);
for (int i = 0; i < dx1;) {
t1xp = 0; t2xp = 0;
if (t1x < t2x) { minx = t1x; maxx = t2x; }
else { minx = t2x; maxx = t1x; }
// process first line until y value is about to change
next0:
while (i < dx1) {
i++;
e1 += dy1;
while (e1 >= dx1) {
e1 -= dx1;
if (changed1) t1xp = signx1;//t1x += signx1;
else {
break next0;
}
}
if (changed1) break;
else t1x += signx1;
}
// Move line
next1:
// process second line until y value is about to change
while (true) {
e2 += dy2;
while (e2 >= dx2) {
e2 -= dx2;
if (changed2) t2xp = signx2;//t2x += signx2;
else {
break next1;
}
}
if (changed2) break;
else t2x += signx2;
}
if (minx > t1x) minx = t1x;
if (minx > t2x) minx = t2x;
if (maxx < t1x) maxx = t1x;
if (maxx < t2x) maxx = t2x;
drawline(minx, maxx, y, col); // Draw line from min to max points found on the y
// Now increase y
if (!changed1) t1x += signx1;
t1x += t1xp;
if (!changed2) t2x += signx2;
t2x += t2xp;
y += 1;
if (y == y2) break;
}
}
// Second half
dx1 = (int)(x3 - x2); if (dx1 < 0) { dx1 = -dx1; signx1 = -1; }
else signx1 = 1;
dy1 = (int)(y3 - y2);
t1x = (int)x2;
if (dy1 > dx1) { // swap values
temp=dy1;dx1=dy1;dy1=temp;
changed1 = true;
}
else changed1 = false;
e1 = (int)(dx1 >> 1);
for (int i = 0; i <= dx1; i++) {
t1xp = 0; t2xp = 0;
if (t1x < t2x) { minx = t1x; maxx = t2x; }
else { minx = t2x; maxx = t1x; }
// process first line until y value is about to change
next3:
while (i < dx1) {
e1 += dy1;
while (e1 >= dx1) {
e1 -= dx1;
if (changed1) { t1xp = signx1; break; }//t1x += signx1;
else break next3;
}
if (changed1) break;
else t1x += signx1;
if (i < dx1) i++;
}
next4:
// process second line until y value is about to change
while (t2x != x3) {
e2 += dy2;
while (e2 >= dx2) {
e2 -= dx2;
if (changed2) t2xp = signx2;
else break next4;
}
if (changed2) break;
else t2x += signx2;
}
if (minx > t1x) minx = t1x;
if (minx > t2x) minx = t2x;
if (maxx < t1x) maxx = t1x;
if (maxx < t2x) maxx = t2x;
drawline(minx, maxx, y,col);
if (!changed1) t1x += signx1;
t1x += t1xp;
if (!changed2) t2x += signx2;
t2x += t2xp;
y += 1;
if (y > y3) return;
}
}
public void Clear(Color col){
for (int y=0;y<JavaProjectTemplate.WINDOW_HEIGHT;y++) {
for (int x=0;x<JavaProjectTemplate.WINDOW_WIDTH;x++) {

Loading…
Cancel
Save