Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-19-2008, 03:34 AM
Member
 
Join Date: Dec 2007
Posts: 4
Ace_Of_John is on a distinguished road
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.
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-19-2008, 11:24 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,124
hardwired is on a distinguished road
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; } }; }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Math levent Java Tutorials 1 05-12-2008 10:03 AM
Math.Random Java Tip Java Tips 0 11-23-2007 03:09 PM
Reading in data from file line by line bluekswing New To Java 1 10-02-2007 01:19 AM
Help with math in java fernando New To Java 1 08-06-2007 07:05 AM
Date math orchid New To Java 2 04-18-2007 08:01 AM


All times are GMT +3. The time now is 01:03 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org