SigRenderer2/src/sig/utils/DrawUtils.java

209 lines
6.4 KiB
Java
Raw Normal View History

2021-11-02 09:41:33 +09:00
package sig.utils;
import java.awt.Color;
import java.util.Arrays;
2021-11-02 09:41:33 +09:00
import sig.SigRenderer;
public class DrawUtils {
static void drawLine(int[] canvas,int sx,int ex,int ny,Color col) {
for (int i=sx;i<=ex;i++) {
Draw(canvas,i,ny,col);
}
}
public static void FillTriangle(int[] canvas,int x1, int y1, int x2, int y2, int x3, int y3, Color col)
{
int t1x=0, t2x=0, y=0, minx=0, maxx=0, t1xp=0, t2xp=0;
boolean changed1 = false;
boolean changed2 = false;
int signx1=0, signx2=0, dx1=0, dy1=0, dx2=0, dy2=0;
int e1=0, e2=0;
// Sort vertices
if (y1>y2) {int t=y1;y1=y2;y2=t;t=x1;x1=x2;x2=t;}
if (y1>y3) {int t=y1;y1=y3;y3=t;t=x1;x1=x3;x3=t;}
if (y2>y3) {int t=y2;y2=y3;y3=t;t=x2;x2=x3;x3=t;}
t1x = t2x = x1; y = 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) { // swap values
int t=dx1;dx1=dy1;dy1=t;
changed1 = true;
}
if (dy2 > dx2) { // swap values
int t=dy2;dy2=dx2;dx2=t;
changed2 = true;
}
e2 = (int)(dx2 >> 1);
// Flat top, just process the second half
boolean goNext=false;
if (y1 == y2) goNext=true;
if (!goNext) {
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
loop3:
while (i<dx1) {
i++;
e1 += dy1;
while (e1 >= dx1) {
e1 -= dx1;
if (changed1) t1xp = signx1;//t1x += signx1;
else break loop3;
}
if (changed1) break;
else t1x += signx1;
}
// Move line
// process second line until y value is about to change
loop2:
while (true) {
e2 += dy2;
while (e2 >= dx2) {
e2 -= dx2;
if (changed2) t2xp = signx2;//t2x += signx2;
else break loop2;
}
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(canvas,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 = x2;
if (dy1 > dx1) { // swap values
int t=dy1;dy1=dx1;dx1=t;
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
loop3:
while (i<dx1) {
e1 += dy1;
while (e1 >= dx1) {
e1 -= dx1;
if (changed1) { t1xp = signx1; break; }//t1x += signx1;
else break loop3;
}
if (changed1) break;
else t1x += signx1;
if (i<dx1) i++;
}
// process second line until y value is about to change
loop2:
while (t2x != x3) {
e2 += dy2;
while (e2 >= dx2) {
e2 -= dx2;
if (changed2) t2xp = signx2;
else break loop2;
}
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(canvas,minx, maxx, y,col);
if (!changed1) t1x += signx1;
t1x += t1xp;
if (!changed2) t2x += signx2;
t2x += t2xp;
y += 1;
if (y>y3) return;
}
}
2021-11-02 09:41:33 +09:00
public static void DrawTriangle(int[]canvas,int x1,int y1,int x2,int y2,int x3,int y3,Color col) {
DrawLine(canvas,x1,y1,x2,y2,col);
DrawLine(canvas,x2,y2,x3,y3,col);
DrawLine(canvas,x3,y3,x1,y1,col);
}
public static void DrawLine(int[] canvas,int x1,int y1,int x2,int y2,Color col) {
int x,y,dx,dy,dx1,dy1,px,py,xe,ye,i;
dx=x2-x1;dy=y2-y1;
dx1=Math.abs(dx);dy1=Math.abs(dy);
px=2*dy1-dx1;py=2*dx1-dy1;
if (dy1<=dx1) {
if (dx>=0) {
x=x1;y=y1;xe=x2;
} else {
x=x2;y=y2;xe=x1;
}
Draw(canvas,x,y,col);
for (i=0;x<xe;i++) {
x=x+1;
if (px<0) {
px=px+2*dy1;
} else {
if ((dx<0&&dy<0)||(dx>0&&dy>0)) {
y=y+1;
} else {
y=y-1;
}
px=px+2*(dy1-dx1);
}
Draw(canvas,x,y,col);
}
} else {
if (dy>=0) {
x=x1;y=y1;ye=y2;
} else {
x=x2;y=y2;ye=y1;
}
Draw(canvas,x,y,col);
for (i=0;y<ye;i++) {
y=y+1;
if (py<=0) {
py=py+2*dx1;
} else {
if ((dx<0&&dy<0)||(dx>0&&dy>0)) {
x=x+1;
} else {
x=x-1;
}
py=py+2*(dx1-dy1);
}
Draw(canvas,x,y,col);
}
}
}
public static void Draw(int[] canvas,int x,int y,Color col) {
if (x>=0&&y>=0&&x<SigRenderer.SCREEN_WIDTH&&y<SigRenderer.SCREEN_HEIGHT) {
//System.out.println(x+","+y);
canvas[x+y*SigRenderer.SCREEN_WIDTH]=col.getRGB();
}
}
}