Die Lösung implementiert nur drawSmoothLine und benutzt double statt int in den Parametern (was mir aber nicht viel gebracht hat, weil ich dann bei kleinen Breiten Sampling-Probleme bekommen habe 😐 )
Dank an Felix Monninger für den Quellcode (den ich dann noch etwas aufpoliert und umgeschrieben um auch noch einen anderen Lösungsansatz für drawLines zu implementieren – der auch eleganter als die Musterlösung ist )
Aufgabe3X.java
public class Aufgabe3X {
// needed for drawPoint
static int width;
static int height;
static int clamp(int min, int val, int max) {
if(val < min)
return min;
else if(val > max)
return max;
else return val;
}
// overloaded function clamp
static double clamp(double min, double val, double max) {
if(val < min)
return min;
else if(val > max)
return max;
else return val;
}
static int validX( int x ) {
return clamp( 0, x, width - 1 );
}
static int validY( int y ) {
return clamp( 0, y, height - 1 );
}
static void drawPoint(double[][] image, double color, double x, double y, double width) {
final double halfWidth = width * 0.5;
final int minX = validX( (int) Math.floor( x - halfWidth ) );
final int minY = validY( (int) Math.floor( y - halfWidth ) );
final int maxX = validX( (int) Math.ceil( x + halfWidth ) );
final int maxY = validY( (int) Math.ceil( y + halfWidth ) );
for(int i = minX ; i <= maxX ; ++i) {
for(int j = minY; j <= maxY; ++j) {
final double distanceToCenter = Math.sqrt((i-x)*(i-x)+(j-y)*(j-y));
image[i][j] += color * (1.0 - clamp(0.0, distanceToCenter / halfWidth, 1.0));
}
}
}
static void drawSmoothLine(double[][] image, double color, double x1, double y1, double x2, double y2, double width) {
final double dx = x2 - x1, dy = y2 - y1;
final double abs_dx = Math.abs( dx ), abs_dy = Math.abs( dy );
final int maxDelta = (int) Math.ceil( Math.max( abs_dx, abs_dy ) );
final double xStep = dx / maxDelta;
final double yStep = dy / maxDelta;
double x = x1;
double y = y1;
for( int i = 0 ; i < maxDelta ; i++ ) {
// hmm have to cast to int, otherwise sampling issues/Moire for small widths
drawPoint( image, color, (int) x, (int) y, width );
x += xStep;
y += yStep;
}
}
public static void main(String[] args) {
// determine optimal width and height
int[] data = Canvas.info1();
for(int i = 0; i < data.length; i += 4) {
width = Math.max( width, Math.max( Canvas.info1()[i], Canvas.info1()[i+2] ) );
height = Math.max( height, Math.max( Canvas.info1()[i+1], Canvas.info1()[i+3] ) );
}
width += 50;
height += 50;
double[][] imageMem = new double[width][height];
// zeichnet schriftzug in Dicke 7
for(int i = 0; i < Canvas.info1().length; i += 4) {
drawSmoothLine(imageMem, 1.0, Canvas.info1()[i], Canvas.info1()[i+1],
Canvas.info1()[i+2], Canvas.info1()[i+3], 7.0);
}
// zeichnet unterschiedlich dicke linien
/*drawSmoothLine(imageMem, 1.0, 90, 180, 270, 250, 30);
drawSmoothLine(imageMem, 1.0, 90, 250, 270, 180, 30);
drawSmoothLine(imageMem, 1.0, 90, 80, 270, 150, 3);
drawSmoothLine(imageMem, 1.0, 90, 150, 270, 80, 3);
drawSmoothLine(imageMem, 1.0, 90, 280, 270, 350, 40);
drawSmoothLine(imageMem, 1.0, 90, 350, 270, 280, 40);*/
Canvas.display(imageMem);
}
}
[/sourcecode]