Results 1 to 2 of 2
  1. #1
    Ace_Of_John is offline Member
    Join Date
    Dec 2007
    Posts
    4
    Rep Power
    0

    Default line plotting math

    Hi im sure ive put this post in the right place as it is about 2d graphics but not the actual graphic functions supplied by java. infact im trying out writing a line algerithm. below is what i have so far, it should work for 25% of line angles. but it doesnt. can someone please point me the right way. thankyou.
    Java Code:
        private void drawLine(int x1, int y1, int x2, int y2)
        {
    	int dX = 0, dY = 0;//device x and y
    	int q = Math.min(x1, x2);//
    	//			 //top left hand side
    	int p = Math.min(y1, y2);//
    	//Calculate the slope
    	float m = (float) (y1 - y2) / (float) (x1 - x2);
    	if (m > 0)// posetive slope rises left to right
    	{
    	    if (y1 - y2 >= x1 - x2)//taller so dify*pixels
    	    {
    		for (float y = 0; y <= y1 - y2; y++)
    		{
    		    dY = GMath.floatToInt(y + p);//(Math.min(y1,y2));
    		    dX = GMath.floatToInt((y * m) + q);
    		    g.drawLine(dX, dY, dX, dY);
    
    		}
    	    }
    	    else//wider so difx*pixels
    	    {
    
    	    }
    	}
    	else //negative slope // falls left to right
    	{
    	    if (x1 - x2 <= y1 - y2)//taller so dify*pixels
    	    {
    
    	    }
    	    else//wider so difx*pixels
    	    {
    
    	    }
    	}
        }
    thankyou for your help.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class LineTest extends JPanel
    {
        Point p1 = new Point();
        Point p2 = new Point();
    
        protected void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setPaint(Color.red);
            g2.fillOval(p1.x-2, p1.y-2, 4, 4);
            g2.setPaint(Color.green.darker());
            g2.fillOval(p2.x-2, p2.y-2, 4, 4);
            g2.setPaint(Color.blue);
            drawLine(p1.x, p1.y, p2.x, p2.y, g2);
        }
    
        private void drawLine(int x1, int y1, int x2, int y2, Graphics2D g)
        {
    	int dX = 0, dY = 0;   //device x and y
    	int xMin = Math.min(x1, x2);
    	int yMin;             // Everything is upside-down.
    	//Calculate the slope
            // Origin of this is at upper left; origin of
            // cartesian coordinate system is at lower left.
            // Therefore, reverse the sign for the rise:
            double rise = -(double)(y2 - y1);
            double run  =  (double)(x2 - x1);
    	double slope = rise / run;
            System.out.printf("slope = %.3f  rise = %.0f  run = %.0f%n",
                               slope, rise, run);
    	if (slope > 0)        // posetive slope rises left to right
    	{
                yMin = Math.max(y1, y2);
    	    if (Math.abs(rise) >= Math.abs(run))  // slope > 1.0
    	    {
    		for (int y = 0; y <= Math.abs(rise); y++)
    		{
    		    dY = yMin - y;
    		    dX = (int)(xMin + (y/slope));
    		    g.drawOval(dX-2, dY-2, 4, 4);
    		}
    	    }
                else  // 0 < slope < 1.0
                {
                    for(int x = 0; x <= Math.abs(run); x++)
                    {
                        dY = (int)(yMin - (slope*x));
                        dX = xMin + x;
                        g.drawOval(dX-2, dY-2, 4, 4);
                    }
                }
    	}
        }
    
        public static void main(String[] args)
        {
            LineTest test = new LineTest();
            test.addMouseListener(test.ma);
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(test);
            f.setSize(400,400);
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        }
    
        private MouseAdapter ma = new MouseAdapter() {
            int pointCount = 0;
    
            public void mousePressed(MouseEvent e) {
                Point p = e.getPoint();
                switch(pointCount) {
                    case 0:
                        p1 = p;
                        break;
                    case 1:
                        p2 = p;
                }
                System.out.printf("p1 = [%d, %d]  p2 = [%d, %d]%n",
                                   p1.x, p1.y, p2.x, p2.y);
                repaint();
                pointCount++;
                if(pointCount > 1)
                    pointCount = 0;
            }
        };
    }

Similar Threads

  1. Java Math
    By levent in forum Java Tutorial
    Replies: 1
    Last Post: 05-12-2008, 10:03 AM
  2. Math.Random
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-23-2007, 03:09 PM
  3. Reading in data from file line by line
    By bluekswing in forum New To Java
    Replies: 1
    Last Post: 10-02-2007, 01:19 AM
  4. Help with math in java
    By fernando in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 07:05 AM
  5. Date math
    By orchid in forum New To Java
    Replies: 2
    Last Post: 04-18-2007, 08:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •