Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (511)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  line algorithm help  (Read 764 times)
0 Members and 1 Guest are viewing this topic.
Offline zulo

Junior Duke




Java games rock!


« Posted 2005-02-14 19:46:51 »

I use bufferedimage and have methods to
put a pixel in the image ( edit a value in the array), and i have a myDrawLine(int x,int y) method that draws a line using the pixel method. However it doesnt work as expected, the line drawn has space in it..can someone post an algorithm for drawing a line just by filling in the pixels representing the line?

this is for the 4k competition game im trying to make, i really would like to get this to work by myself but..:/ im not good enough Roll Eyes

ive been looking for a basic tutorial on graphics with this kind of algorithm explained and from what i learned i created this method:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
public static void myDrawLine(int x1, int y1, int x2, int y2)
            {
                  int deltax = x2 - x1;           // The difference in the x's
                  int deltay = y2 - y1;           // The difference in the y's
                  int y = y1;                     // Start y off at the first pixel value
                  int ynum = deltax / 2;          // The starting value for the numerator
                  for (int x = x1; x <= x2; x++)
                  {
                    Point(x, y);           // Draw the current pixel
                    ynum += deltay;           // Increase the numerator by the top of the fraction
                    if (ynum >= deltax)       // Check if numerator >= denominator
                    {
                      ynum -= deltax;         // Calculate the new numerator value
                      y++;                    // Increase the value in front of the numerator (y)
                    }
                  }
}


but the line created looks incomplete drawn at some
angles..i know this works only when the x coordinate is smaller then the x2 coordinate..

maybe i should just use the Graphics.drawline() method? would save some space probably
Offline tom
« Reply #1 - Posted 2005-02-14 20:12:29 »

You need 2 for loops. One that iterates vertically (y) when deltay > deltax, and one wich iterates horisontally (x) when deltay <= deltax.

Offline oNyx

JGO Coder


Medals: 2


pixels! :x


« Reply #2 - Posted 2005-02-14 20:44:10 »

Pick one...
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
public void brese(Graphics g, int x0, int y0, int x1, int y1)
{
      if(x0>x1)
      {
            int s=x0;
            x0=x1;x1=s;
            s=y0;
            y0=y1;y1=s;
      }
      boolean steep=Math.abs(y1-y0)>Math.abs(x1-x0);
      if(steep)
      {
            int s=x0;
            x0=x1;x1=s;
            s=y0;
            y0=y1;y1=s;
      }
      int deltax=x1-x0;
      int deltay=Math.abs(y1-y0);
      int error=0;
      int deltaerr=deltay;
      int y=y0;
      int ystep=y0<y1?1:-1;
      for(int x=x0;x<=x1;x++)
      {
            if(steep)
                  plot(g,y,x);
            else
                  plot(g,x,y);
            error+=deltaerr;
            if(error*2>=deltax)
            {
                  y+=ystep;
                  error-=deltax;
            }
      }
}
public void brese2(Graphics g, int x0, int y0, int x1, int y1)
{
      int dy = y1 - y0;
      int dx = x1 - x0;
      if(dx == 0 && dy == 0)
            return;
      double tX = x0;
      double tY = y0;
      if(Math.abs(dy) > Math.abs(dx))
      {
            int ystep = y1 <= y0 ? -1 : 1;
            double d = (double)dx / (double)dy;
            for(int c = 0; c <= Math.abs(dy); c++)
            {
                  plot(g,(int)Math.round(tX),(int)Math.round(tY));
                  tX += d * (double)ystep;
                  tY += ystep;
            }

      } else
      {
            int xstep = x1 <= x0 ? -1 : 1;
            double d = (double)dy / (double)dx;
            for(int c = 0; c <= Math.abs(dx); c++)
            {
                  int y = (int)Math.round(tY);
                  plot(g,(int)Math.round(tX),(int)Math.round(tY));
                  tY += d * (double)xstep;
                  tX += xstep;
            }

      }
}
public void brese3(Graphics g, int x0, int y0, int x1, int y1)
{
      int dy = y1 - y0;
      int dx = x1 - x0;
      double tX = x0;
      double tY = y0;

      int xstep = x1 <= x0 ? -1 : 1;
      int ystep = y1 <= y0 ? -1 : 1;
      double d;
      boolean ys;
      if(Math.abs(dy) > Math.abs(dx))
      {
            d = (double)dx / (double)dy;
            ys = true;
      }
      else
      {
            d = (double)dy / (double)dx;
            ys = false;
      }
      int cx=x0, cy=y0;
      boolean hack=true;
      do
      {
            if(!hack)
            {
                  if(ys)
                  {
                        tX += d * (double)ystep;
                        tY += ystep;
                  }
                  else
                  {
                        tY += d * (double)xstep;
                        tX += xstep;
                  }
                  cx=(int)Math.round(tX);
                  cy=(int)Math.round(tY);
            }
            hack=false;
            plot(g,cx,cy);
      }while(cx!=x1||cy!=y1);
}

弾幕 ☆ @mahonnaiseblog
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (50 views)
2014-10-17 03:59:02

Norakomi (39 views)
2014-10-16 15:22:06

Norakomi (31 views)
2014-10-16 15:20:20

lcass (36 views)
2014-10-15 16:18:58

TehJavaDev (66 views)
2014-10-14 00:39:48

TehJavaDev (65 views)
2014-10-14 00:35:47

TehJavaDev (56 views)
2014-10-14 00:32:37

BurntPizza (72 views)
2014-10-11 23:24:42

BurntPizza (44 views)
2014-10-11 23:10:45

BurntPizza (84 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!