📄 rayinnabox.java
字号:
package ergo.ui;
// $Id: RayInnaBox.java,v 1.3 1999/08/15 01:40:29 sigue Exp $
/*
* Copyright (C) 1999 Carl L. Gay and Antranig M. Basman.
* See the file copyright.txt, distributed with this software,
* for further information.
*/
public class RayInnaBox {
public static void params(int baseb1, int shadeb1, int specb1,
int basew1, int shadew1, int specw1) {
// baseb = baseb1; shadeb = shadeb1; specb = specb1;
// basew = basew1; shadew = shadew1; specw = specw1;
}
static int baseb = 20, shadeb = 40, specb = 115, attenb = 20;
static int basew = 200, shadew = 20, specw = 35, attenw = 30;
private static Vector3 normal = new Vector3();
private static Vector3 ez = new Vector3(0, 0, 1);
private static Vector3 temp = new Vector3();
private static double r2 = Math.sqrt(2);
private static final int zazzit(int edgecolor, int backcolor, double xc, double yc, double step) {
int tr=0, tg=0, tb=0, ta=0;
int ti=0;
double x = 2*xc/step; double y = 2*yc/step;
double r = 1/step /* - 1*/;
double r2 = r*r;
double d = ((x*x)/4) + ((y*y)/4) - r2; // When this is positive, we are outside.
if (d < 0) ++ti;
d += -y + 1; y -= 2.0; // TOP
if (d < 0) ++ti;
d += x + 1; x += 2.0; // TOP RIGHT
if (d < 0) ++ti;
d += y + 1; y += 2.0; // RIGHT
if (d < 0) ++ti;
d += y + 1; y += 2.0; // BOTTOM RIGHT
if (d < 0) ++ti;
d += -x + 1; x -= 2.0; // BOTTOM
if (d < 0) ++ti;
d += -x + 1; x -= 2.0; // BOTTOM LEFT
if (d < 0) ++ti;
d += -y + 1; y -= 2.0; // LEFT
if (d < 0) ++ti;
d += -y + 1; y -= 2.0; // TOP LEFT
if (d < 0) ++ti;
// System.out.print(ti + " ");
// and we finish on a Felix special...!
int fac = 255 - 255*ti/9; // number of inside pixels.
// fac = 0;
return ((( ((((backcolor & 0xff0000) - (edgecolor & 0xff0000))*fac)& 0xff000000) +
((((backcolor & 0xff00) - (edgecolor & 0xff00))*fac)& 0xffff0000) +
(((backcolor & 0xff) - (edgecolor & 0xff))*fac))>>8) + edgecolor);
}
public static RawImage blackStone(int side, int color, int basecolor, int alpha, double radadjust) {
int base, shade, spec, atten;
if (color == 0) {
base = baseb; shade = shadeb; spec = specb; atten = attenb;
}
else {
base = basew; shade = shadew; spec = specw; atten = attenw;
}
int alphahead = alpha<<24;
long now = System.currentTimeMillis();
RawImage br = new RawImage(side, side);
double center = ((double)side - 1)/2;
double radius = center + 0.5;
radius += radadjust;
double maxdrift = 2*r2/(center*3); // linear approximation will be good enough
Vector3 incident = new Vector3(-1.5, -1.5, 1).norm();
for (int y=side-1; y>=0; --y) {
for (int x=side-1; x>=0; --x) {
double xc = ((double)x - center)/radius;
double yc = ((double)y - center)/radius;
int value = 0;
double ex2y2 = xc*xc + yc*yc;
if (ex2y2 < (1 /*+maxdrift*/)) { // within outer ring.
double zc = Math.sqrt(1 - ex2y2);
normal.imbue(xc, yc, zc); // automatically normalised.
double cos = normal.dot(incident);
Vector3.scalehorr(temp, 2*cos, normal);
Vector3.subhorr(temp, temp, incident);
double cos2 = Math.max(0, ez.dot(temp));
cos2 = cos2*cos2; // now cos2
double cos4 = cos2*cos2; // now cos4
// cos4 = cos4a*cos4a; // now cos8
double frontal = ex2y2 * ex2y2;
frontal *= frontal;
// if (color == 1) {
// frontal *= frontal;
// }
if (color == 0) cos2 = cos2*cos4*cos4; // now cos10 - black.
else cos2 = cos2*cos4*cos4;
// else cos4 = cos4a;
// System.out.println(cos);
value = base + (int)(shade*Math.max(0,cos - frontal/2)) + (int)(spec*cos2) - (int)(frontal*atten);
value = (value*0x010101)|alphahead;
if (ex2y2 > (1-2*maxdrift)) { // Special code for antialiasing edges.
value = zazzit(value, basecolor, xc, yc, 1/center) | alphahead;
}
else {
// System.out.print("I ");
}
}
else {
// System.out.print("O ");
}
br.setpixel(value, x, y);
}
// System.out.println();
}
// System.out.println(System.currentTimeMillis()-now);
return br;
}
}
/* Test harness for ood and stones */
/*
class stest extends Panel {
Image wstone;
Image bstone;
Image wsstone;
Image bwstone;
Image abwstone;
Image blankb;
Image lineb;
Image polyb;
Image bstoneb;
Image wstoneb;
Image oodb;
Color oback = new Color(229, 188, 102);
Color back = new Color(213, 182, 85);
int osize;
stest() {
int size = 55;
osize=size;
Circle circle = new Circle();
Line line = new Line();
PolygonBr poly = new PolygonBr();
Color insidecolor = Color.white;
Color rimcolor = Color.black;
int stoneDiameter = size-1 ;
RawImage wsbit = new RawImage(size, size);
circle.fillcirc(wsbit, insidecolor.getRGB(), stoneDiameter);
circle.emptycirc(wsbit, rimcolor.getRGB(), stoneDiameter);
RawImage blank = new RawImage(size, size).blank(back.getRGB());
RawImage wssbit = ImArith.ap(blank, wsbit);
wstone = createImage(wsbit.mis());
wsstone = createImage(wssbit.mis());
long now = System.currentTimeMillis();
size *= 3;
stoneDiameter = size - 2;
RawImage bwsbit = new RawImage(size, size);
circle.fillcirc(bwsbit, ImArith.tp(rimcolor, 128), stoneDiameter);
circle.fillcirc(bwsbit, ImArith.tp(insidecolor, 128), stoneDiameter-6);
RawImage wstone = ImArith.antin(bwsbit,3);
// RawImage bblank = new RawImage(size, size).blank(back.getRGB());
RawImage sblank = wstone.bclone().blank(back.getRGB());
// RawImage bsplat = ImArith.ap(bblank, bwsbit);
// RawImage abwsbit = ImArith.antin(bsplat, 3);
RawImage abwsbit = ImArith.ap(sblank, wstone);
System.out.println("Big time: "+(System.currentTimeMillis()-now));
// System.out.println(" "+bblank.data[0] + " "+ abwsbit.data[0] + " "+(bblank.data[0]>>24));
RawImage linetest = new RawImage(100,100);
RawImage polytest = new RawImage(21,21);
// line.line(linetest, 0xffffffff, 50,50,100,50);
// line.line(linetest, 0xffffff00, 50,50,50,100);
// line.line(linetest, 0xffff00ff, 50,50,0,50);
// line.line(linetest, 0xffff0000, 50,50,50,0);
// line.line(linetest, 0xff00ffff, 50,50,100,60);
// line.line(linetest, 0xff00ff00, 50,50,60,100);
line.line(linetest, 0xff0000ff, 50,50,25,100);
line.line(linetest, 0xffffffff, 25,100,50,50);
poly.init(4);
poly.add(5,5).add(15,5).add(15,15).add(5,15);
poly.fill(polytest, 0xffffffff);
poly.init(4);
poly.add(7,7).add(13,7).add(13,13).add(7,13);
poly.fill(polytest, 0);
System.out.println(polytest);
bwstone = createImage(bwsbit.mis());
abwstone = createImage(abwsbit.mis());
blankb = createImage(blank.mis());
lineb = createImage(linetest.mis());
polyb = createImage(polytest.mis());
setLayout(null);
add(basebfield);
add(shadebfield);
add(specbfield);
add(basewfield);
add(shadewfield);
add(specwfield);
add(redrawbut);
generate();
}
boolean there = false;
private void showFields() {
there = true;
int width = basebfield.getPreferredSize().width;
int height = basebfield.getPreferredSize().height;
int bx = 10; int by = 330; int dx = width *6/5; int dy = height*6/5;
basebfield.setBounds(bx,by, width, height);
basebfield.show();
shadebfield.setBounds(bx + dx,by, width, height);
shadebfield.show();
specbfield.setBounds(bx + dx*2,by, width, height);
specbfield.show();
basewfield.setBounds(bx,by+dy, width, height);
basewfield.show();
shadewfield.setBounds(bx + dx,by+dy, width, height);
shadewfield.show();
specwfield.setBounds(bx + dx*2,by+dy, width, height);
specwfield.show();
redrawbut.setBounds(bx + dx*3, by + dy/2, redrawbut.getPreferredSize().width,
redrawbut.getPreferredSize().height);
System.out.println("There");
// validate();
}
public boolean action(Event e, Object o) {
generate();
return true;
}
private void generate() {
int baseb = Integer.parseInt(basebfield.getText());
int shadeb = Integer.parseInt(shadebfield.getText());
int specb = Integer.parseInt(specbfield.getText());
int basew = Integer.parseInt(basewfield.getText());
int shadew = Integer.parseInt(shadewfield.getText());
int specw = Integer.parseInt(specwfield.getText());
RayInnaBox.params(baseb, shadeb, specb, basew, shadew, specw);
bstoneb = createImage(RayInnaBox.blackStone(25,0, back.getRGB(), 255).mis());
wstoneb = createImage(RayInnaBox.blackStone(25,1, back.getRGB(), 255).mis());
oodb = createImage(OodInnaBox.Ood(512, 4).mis());
System.out.println("Generated");
repaint();
}
static int baseb = 50, shadeb = 70, specb = 50;
static int basew = 180, shadew = 10, specw = 65;
private Button redrawbut = new Button("Draw");
private TextField basebfield = new TextField("30", 3);
private TextField shadebfield = new TextField("40", 2);
private TextField specbfield = new TextField("120", 2);
private TextField basewfield = new TextField("220", 3);
private TextField shadewfield = new TextField("10", 2);
private TextField specwfield = new TextField("25", 2);
public Dimension getMinimumSize() {return new Dimension(800,700);}
public Dimension getPreferredSize() {return new Dimension(800,700);}
public void update(Graphics g) {
if (!there) { showFields();}
g.setColor(back);
Rectangle r = g.getClipBounds();
g.fillRect(r.x, r.y, r.width, r.height);
g.drawImage(wstone, 10, 10, this);
g.drawImage(wsstone, 10+osize, 10, this);
g.drawImage(bwstone, 10, 150, this);
g.drawImage(blankb, 150, 10, this);
g.drawImage(abwstone, 10, 10+osize, this);
g.drawImage(abwstone, 10+osize, 10+osize, this);
g.drawImage(lineb, 150, 10, this);
g.drawImage(polyb, 150, 150, this);
g.drawImage(bstoneb, 170, 80, this);
g.drawImage(wstoneb, 390, 80, this);
g.drawImage(oodb, 300, 300, this);
}
public void paint(Graphics g) {
update(g);
}
public static void main(String[] argv) {
Frame f = new Frame();
stest s = new stest();
f.setLayout(new BorderLayout());
f.add("Center", s);
f.invalidate();
// s.setSize(200,200);
f.pack();
f.show();
}
}
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -