gotm 0 Light Poster

So we have to implement Bresenhams line drawing algorithm as well as Cohen-Sutherland line clipping algorithm.

For some reason my whole program (9 classes, but only will include Line.java here) runs fine, but the output .xpm file contains a picture that is not what it's supposed to look like from the input .ps file. Some lines aren't appearing, some appear but are clipped wrong. I'm assuming it's a problem with my algorithms so I was wondering if I could get some feedback.

If you guys need to see more of my code let me know as I have it all.

//
// Line class.
//
public class Line
{
	// lines have two points that make them up
	private Point _a;
	private Point _b;
	
	// for Cohen Sutherland clipping
	private int RIGHT = 2;
    private int TOP = 8;
    private int BOTTOM = 4;
    private int LEFT = 1;
	
	/**
	 * Constructor takes in two points.
	 * 
	 * @param Point a
	 * @param Point b
	 */
	public Line(Point a, Point b)
	{
		_a = a;
		_b = b;
	}
	
	/**
	 * Getters and setters.
	 */
	public Point getFirstPoint()
	{
		return _a;
	}
	
	public Point getSecondPoint()
	{
		return _b;
	}
	
	public void setFirstPoint(Point a)
	{
		_a = a;
	}
	
	public void setSecondPoint(Point b)
	{
		_b = b;
	}
	
	public void displayLine()
	{
		System.out.println(this.getFirstPoint().getX() + "," + this.getFirstPoint().getY());
		System.out.println(this.getSecondPoint().getX() + "," + this.getSecondPoint().getY());
	}
	
	/**
	 * DDA Algorithm for drawing Lines
	 * 
	 * @param FrameBuffer pixelMap
	 */
	public void DDA(FrameBuffer pixelMap)
	{
		int x1 = this.getFirstPoint().getX();
		int y1 = this.getFirstPoint().getY();
		int x2 = this.getSecondPoint().getX();
		int y2 = this.getSecondPoint().getY();
		
		double m = (double)(y2-y1)/(x2-x1);
		double y = (double)y1;
		
		int iy;
		
		for (int x = x1 ; x <= x2 ; x++) {
			iy = (int)Math.round(y);
		    pixelMap.setPixel(x, iy);
		    y += m;
		}
	} 
	
	public int ComputeOutCode (int x, int y, int xmin, int ymin, int xmax, int ymax)
    {
        int code = 0;
        if (y > ymax)
            code |= TOP;
        else if (y < ymin)
            code |= BOTTOM;
        if (x > xmax)
            code |= RIGHT;
        else if (x < xmin)
            code |= LEFT;
        return code;
    }
	
	public void cohenSutherland(int xmin, int ymin, int xmax, int ymax)
    {
		
		int x1 = this.getFirstPoint().getX();
		int y1 = this.getFirstPoint().getY();
		int x2 = this.getSecondPoint().getX();
		int y2 = this.getSecondPoint().getY();
		
        //Outcodes for P0, P1, and whatever point lies outside the clip rectangle
        int outcode0, outcode1, outcodeOut, hhh = 0;
        boolean accept = false, done = false;
 
        //compute outcodes
        outcode0 = ComputeOutCode (x1, y1, xmin, ymin, xmax, ymax);
        outcode1 = ComputeOutCode (x2, y2, xmin, ymin, xmax, ymax);
 
        //System.out.println( outcode0 + " " + outcode1 );
 
        do{
            if ((outcode0 | outcode1) == 0 )
            {
                accept = true;
                done = true;
            }
            else if ( (outcode0 & outcode1) > 0 )
            {
                done = true;
            }
 
            else
            {
                //failed both tests, so calculate the line segment to clip
                //from an outside point to an intersection with clip edge
                int x = 0, y = 0;
                //At least one endpoint is outside the clip rectangle; pick it.
                outcodeOut = outcode0 != 0 ? outcode0: outcode1;
                //Now find the intersection point;
                //use formulas y = y0 + slope * (x - x0), x = x0 + (1/slope)* (y - y0)
                if ( (outcodeOut & TOP) > 0 )
                {
                    x = x1 + (x2 - x1) * (ymax - y1)/(y2 - y1);
                    y = ymax;
                }
                else if ((outcodeOut & BOTTOM) > 0 )
                {
                    x = x1 + (x2 - x1) * (ymin - y1)/(y2 - y1);
                    y = ymin;
                }
                else if ((outcodeOut & RIGHT)> 0)
                {
                    y = y1 + (y2 - y1) * (xmax - x1)/(x2 - x1);
                    x = xmax;
                }
                else if ((outcodeOut & LEFT) > 0)
                {
                    y = y1 + (y2 - y1) * (xmin - x1)/(x2 - x1);
                    x = xmin;
                }
                //Now we move outside point to intersection point to clip
                //and get ready for next pass.
                if (outcodeOut == outcode0)
                {
                    x1 = x;
                    y1 = y;
                    outcode0 = ComputeOutCode (x1, y1, xmin, ymin, xmax, ymax);
                }
                else
                {
                    x2 = x;
                    y2 = y;
                    outcode1 = ComputeOutCode (x2, y2, xmin, ymin, xmax, ymax);
                }
            }
            hhh ++;
        }
        while (done  != true && hhh < 5000);
 
 
        if(accept)
        {
            set(x1, y1, x2, y2);
        }
 
    }

	public void set(int x1, int y1, int x2, int y2 )
    {
        this.getFirstPoint().setX(x1);
        this.getFirstPoint().setY(y1);
        this.getSecondPoint().setX(x2);
        this.getSecondPoint().setY(y2);
        return;
    }

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.