draw()
method:
public void draw(Color color, double[] p, double[] q) { double alpha, m; if (q[0] == p[0]) m = Double.POSITIVE_INFINITY; else m = (q[1]-p[1])/(q[0]-p[0]); if ((m >= -1.0) && (m <= 1.0)) { double y; int xEnd, xStart; if (p[0] <= q[0]) { xStart = (int)Math.round(p[0]); xEnd = (int)Math.round(q[0]); } else { xStart = (int)Math.round(q[0]); xEnd = (int)Math.round(p[0]); } for (int x=xStart; x<=xEnd; x++) { if (q[0] == p[0]) { y = p[1]; } else { alpha = (x - p[0])/(q[0] - p[0]); y = p[1] + alpha * (q[1] - p[1]); } fb.setPixel(x, (int)Math.round(y), color); } } else { double x; int yEnd, yStart; if (p[1] <= q[1]) { yStart = (int)Math.round(p[1]); yEnd = (int)Math.round(q[1]); } else { yStart = (int)Math.round(q[1]); yEnd = (int)Math.round(p[1]); } for (int y=yStart; y<=yEnd; y++) { if (q[1] == p[1]) { x = p[0]; } else { alpha = (y - p[1])/(q[1] - p[1]); x = p[0] + alpha * (q[0] - p[0]); } fb.setPixel((int)Math.round(x), y, color); } } }
show (in the grid below) what will be drawn if this method is called as follows:
double[] p = { 0, 0}; double[] q = {10, 3}; draw(Color.RED, p, q);
You must assume that the lower-left pixel is the origin [i.e., (0,0)] and that the coordinates increase from left-to-right and bottom-to-top.
onLine()
method. Your
answer must be consistent with the comments describing the method.
You may use methods that were required in the
VMath
and MMath
classes.
/** * Determines if a point is on a line * (represented in in explicit form) * * @param m The slope of the line * @param b The vertical intercept of the line * @param p The test point * @return true if p is on the line defined by h */ public static boolean onLine(double m, double b, double[] p) { }
sameSide()
method. Your
answer must be consistent with the comments describing the method.
You may use methods that were required in the
VMath
and MMath
classes.
/** * Determines if two 2D points are on the same side of * a line segement * * The vertexes can be assumed to be ordered correctly * (i.e., obey the right-hand rule) * * @param p One point on the line * @param q Another point on the line * @param s One test point * @param t The other test point * @return true if s and t are on the same side; false otherwise */ public static boolean sameSide(double[] p, double[] q, double[] s, double[] t) { }
sameSide()
method above, write a method that
determines if a given point is inside a convex polygon.
Copyright 2007