📄 linedx.java
字号:
package graphics0;
import java.awt.*;
/**
* <p>Title: computer graphics </p>
* <p>Description: homeworks</p>
* <p>Copyright: Copyright (c) 2004 </p>
* <p>Company: shu</p>
* @author dxf
* @version 1.0
*/
public class LineDx
extends Object2D {
Point spoint, epoint;
int DrawMethod;
void drawDemo(Graphics Canvas) {
Point tmp;
switch (DrawMethod) {
case 0: { //linedda
if (Math.abs(spoint.x - epoint.x) > Math.abs(spoint.y - epoint.y)) {
if (spoint.x > epoint.x) {
tmp = new Point(epoint);
epoint = spoint;
spoint = tmp;
}
LineDDAXDemo(spoint.x, spoint.y, epoint.x, epoint.y, Canvas);
}
else {
if (spoint.y > epoint.y) {
tmp = new Point(epoint);
epoint = spoint;
spoint = tmp;
}
LineDDAYDemo(spoint.x, spoint.y, epoint.x, epoint.y, Canvas);
}
break;
}
case 1: { //mid
if (Math.abs(spoint.x - epoint.x) > Math.abs(spoint.y - epoint.y)) {
if (spoint.x > epoint.x) {
tmp = new Point(epoint);
epoint = spoint;
spoint = tmp;
}
MidpointineXDemo(spoint.x, spoint.y, epoint.x, epoint.y, Canvas);
}
else {
if (spoint.y > epoint.y) {
tmp = new Point(epoint);
epoint = spoint;
spoint = tmp;
}
MidpointineYDemo(spoint.x, spoint.y, epoint.x, epoint.y, Canvas);
break;
}
}
case 2: { //Bresenham
if (Math.abs(spoint.x - epoint.x) > Math.abs(spoint.y - epoint.y)) {
if (spoint.x > epoint.x) {
tmp = new Point(epoint);
epoint = spoint;
spoint = tmp;
}
InterBresenhamlineXDemo(spoint.x, spoint.y, epoint.x, epoint.y,
Canvas);
}
else {
if (spoint.y > epoint.y) {
tmp = new Point(epoint);
epoint = spoint;
spoint = tmp;
}
InterBresenhamlineYDemo(spoint.x, spoint.y, epoint.x, epoint.y,
Canvas);
break;
}
}
}
}
////////////////////////////////////////////////////////////////////////////////
void draw(Graphics Canvas) {
Point tmp;
int w1, w2, w;
//////////////////////////////////////
w = LineWidth % 2;
if (1 == w) {
w1 = (LineWidth + 1) / 2;
w2 = 0 - (LineWidth - 1) / 2;
}
else {
w1 = LineWidth / 2;
w2 = 0 - w1;
}
switch (DrawMethod) {
case 0: { //linedda
if (Math.abs(spoint.x - epoint.x) > Math.abs(spoint.y - epoint.y)) {
if (spoint.x > epoint.x) {
tmp = new Point(epoint);
epoint = spoint;
spoint = tmp;
}
LineDDAX(spoint.x, spoint.y, epoint.x, epoint.y, Canvas,
w1, w2);
}
else {
if (spoint.y > epoint.y) {
tmp = new Point(epoint);
epoint = spoint;
spoint = tmp;
}
LineDDAY(spoint.x, spoint.y, epoint.x, epoint.y, Canvas,
w1, w2);
}
break;
}
case 1: { //mid
if (Math.abs(spoint.x - epoint.x) > Math.abs(spoint.y - epoint.y)) {
if (spoint.x > epoint.x) {
tmp = new Point(epoint);
epoint = spoint;
spoint = tmp;
}
MidpointineX(spoint.x, spoint.y, epoint.x, epoint.y,
Canvas, w1, w2);
}
else {
if (spoint.y > epoint.y) {
tmp = new Point(epoint);
epoint = spoint;
spoint = tmp;
}
MidpointineY(spoint.x, spoint.y, epoint.x, epoint.y,
Canvas, w1, w2);
break;
}
}
case 2: { //Bresenham
if (Math.abs(spoint.x - epoint.x) > Math.abs(spoint.y - epoint.y)) {
if (spoint.x > epoint.x) {
tmp = new Point(epoint);
epoint = spoint;
spoint = tmp;
}
InterBresenhamlineX(spoint.x, spoint.y, epoint.x, epoint.y,
Canvas, w1, w2);
}
else {
if (spoint.y > epoint.y) {
tmp = new Point(epoint);
epoint = spoint;
spoint = tmp;
}
InterBresenhamlineY(spoint.x, spoint.y, epoint.x, epoint.y,
Canvas, w1, w2);
break;
}
}
}
}
boolean IsInside(Point p0) {
return (true);
};
public LineDx() {
spoint = new Point(0, 0);
epoint = new Point(0, 0);
DrawMethod = 0;
}
public LineDx(Point s, Point e, int d, Color c, int w, int l) {
spoint = new Point(s);
epoint = new Point(e);
ForeColor = c;
DrawMethod = d;
LineWidth = w;
LineStyle = l;
}
//////////////////////////////////////////////////////////////////////////////
public void LineDDAX(int x0, int y0, int x1, int y1,
Graphics g, int w1, int w2)
/* 假定x0<x1,-1<=k<=1 */
{
int x, t, w, j = 0;
float dx, dy, y, k;
g.setColor(ForeColor);
dx = x1 - x0;
dy = y1 - y0;
k = dy / dx;
y = y0;
////////////////////////////////////
for (x = x0; x <= x1; x++) {
t = Math.round(y);
if (j == 8) {
j = 0;
}
if ('1' == sLineStyle[LineStyle].charAt(j)) {
for (w = w2; w < w1; w++) {
g.drawLine(x, t + w, x, t + w);
}
}
j++;
y += k;
}
}
///////////////////////////////////////////////////////////////////////////////
public void LineDDAY(int x0, int y0, int x1, int y1,
Graphics g, int w1, int w2)
/* 假定-1<=1/k<=1 */
{
int y, t, w, j = 0;
float dx, dy, x, k;
g.setColor(ForeColor);
dx = x1 - x0;
dy = y1 - y0;
k = dx / dy;
x = x0;
//////////////////////////////////////////////////
for (y = y0; y <= y1; y++) {
t = Math.round(x);
if (j == 8) {
j = 0;
}
if ('1' == sLineStyle[LineStyle].charAt(j)) {
for (w = w2; w < w1; w++) {
g.drawLine(t + w, y, t + w, y);
}
}
j++;
x += k;
}
}
////////////////////////////////////////////////////////////////////////////
public void MidpointineX(int x0, int y0, int x1, int y1,
Graphics g, int w1, int w2) {
int a, b, d1, d2, d, x, y, w, j = 1;
g.setColor(ForeColor);
boolean bg;
if (y1 > y0) {
bg = false;
}
else {
bg = true;
}
if (bg) {
a = y1 - y0;
}
else {
a = y0 - y1;
}
b = x0 - x1;
d = 2 * a - b;
d2 = 2 * (a - b);
d1 = 2 * a;
x = x0;
y = y0;
g.drawLine(x, y, x, y);
while (x < x1) {
x++;
if (d < 0) {
if (bg) {
y--;
}
else {
y++;
}
d += d2;
}
else {
d += d1;
}
if (j == 8) {
j = 0;
}
if ('1' == sLineStyle[LineStyle].charAt(j)) {
for (w = w2; w < w1; w++) {
g.drawLine(x, y + w, x, y + w);
}
}
j++;
}
/* while */
}
/* mid pointine */
////////////////////////////////////////////////////////////////////////////
public void MidpointineY(int x0, int y0, int x1, int y1,
Graphics g, int w1, int w2) {
int a, b, d1, d2, d, x, y, j = 1;
g.setColor(ForeColor);
boolean bg;
if (x1 > x0) {
bg = false;
}
else {
bg = true;
}
if (bg) {
a = x1 - x0;
}
else {
a = x0 - x1;
}
b = y0 - y1;
d = 2 * a - b;
d2 = 2 * (a - b);
d1 = 2 * a;
x = x0;
y = y0;
g.drawLine(x, y, x, y);
while (y < y1) {
y++;
if (d < 0) {
if (bg) {
x--;
}
else {
x++;
}
d += d2;
}
else {
d += d1;
}
if (j == 8) {
j = 0;
}
if ('1' == sLineStyle[LineStyle].charAt(j)) {
for (int w = w2; w < w1; w++) {
g.drawLine(x + w, y, x + w, y);
}
}
j++;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -