Login  Register

Re: Nelp needed

Posted by Michael Schmid on Dec 28, 2009; 2:10pm
URL: http://imagej.273.s1.nabble.com/Nelp-needed-tp3689904p3689906.html

Hi Bob,

as I understand it, in effect, the algorithm is the same as you describe
(apart from a problem, see below), but it is implemented in a slightly
different way. See the lineTo method in ImageProcessor.java (current
version at
http://rsb.info.nih.gov/ij/source/ij/process/ImageProcessor.java)

First, the increments in x and y are calculated. They are proportional to
the vector components from the starting point to the end point. The
proportionality factor is chosen such that the increment with the larger
absolute value has an absolute value of 1. Then, starting from the first
pixel, the increment in x and y is added, and the pixel is set at the
rounded value.

Two specialties:
- Lines with a length of more than 1 million pixels in x or y are
considered erroneous (no image is that big) and ignored.

- It seems that there is a bug with negative starting values. Here is an
example macro:
  newImage("test", "8-bit White", 100, 100, 1);
  drawLine(1, -1, 9, 1);
  drawLine(1,  1, 9, 3);
  drawLine(1,  3, 9, 5);
The first (uppermost) line is not parallel to the two first one although
it should be.

I think that the reason for the bug is in macro/Functions.java (adding 0.5
instead of Math.round), but I also do not understand the code in lineTo in
this respect (is the case distinction for positive/negative cx, cy when
calculating the starting point x, y supposed to compensate for the
macro/Functions.java problem?)


Michael
__________________________________________________________________

On Mon, December 28, 2009 05:55, Robert Dougherty wrote:

> Dilip,
>
> Here is my understanding.  I'm sure it will be corrected if necessary.
> Suppose you want to draw a  line from (x1,y2) to (x2,y2).   First
> determine which is larger, deltaX = |x2-x1| or deltaY = |y2 - y1|.  If
> deltaX is larger, then our line is going to have deltaX+1 pixels, with x
> running from x1 to x2.  For each x value on the line (segment), the
> corresponding value of y is computed by interpolating:
>
> y = (int)( y1(x2 - x)/(x2 - x1) + y2(x - x1)/(x2 - x1) + 0.5)
>
> If deltaY is larger, then y runs from y1 to y2, and each x is computed to
> by a formula analogous to the above with x and y interchanged.  If deltaX
> = deltaY, then do it either way.  If the largest delta is 0, then just
> draw one pixel.
>
> In ImageJ programming, you should not have to do this computation if all
> you want to do is draw a line.  There are ImageJ and AWT methods to handle
> it.
>
> Bob
>
> On Dec 27, 2009, at 8:16 PM, Dilip Baluguri wrote:
>
>> I just want to know how a line is drawn between two points. What is I
>> need
>> is how the intermediate pixels are choosen while drawing a line. Given
>> two
>> poins A(10,20) and B(40,200) how intermediate pixels are choosen.Any
>> help is
>> appreciated.
>> Thanks in advance.
>>
>>
>>
>> Dilip Kumar Baluguri
>> CWID: 50044123
>> Graduate Student- Computer Science
>> TAMU-Commerce
>> (630)383-9112
>>
>> Mike Ditka <http://www.brainyquote.com/quotes/authors/m/mike_ditka.html>
>>  -
>> "If God had wanted man to play soccer, he wouldn't have given us arms."
>
> Robert Dougherty, Ph.D.
> President, OptiNav, Inc.
> 4176 148th Ave. NE
> Redmond, WA 98052
> (425)891-4883
> FAX (425)467-1119
> www.optinav.com
> [hidden email]
>