问题描述:

I'm trying to implement the scanline algorithm in Javascript. It looks like my calculation of x is wrong as the lines are to short or to long.

These edges should be filled:

This is how my scanline code fills it:

My edge table has these fields:

`et.push({`

Ymin: Ymin, // minimum Y of the edge

Ymax: Ymax, // maximum Y

x: Xmin, // X value of Ymin point; updated for every scanline

dx: Xmax - Xmin, // Delta X

dy: Ymax - Ymin // Delta Y

});

The new X after every scanline is calculated this way:

`// aet is active edge table`

if (aet[i].dx != 0 && aet[i].dy != 0) {

// this is not a vertical edge

var incr = aet[i].dy / aet[i].dx; // =1/(dx/dy) ... dx/dy=m

if (aet[i].dy > 0) {

incr = incr;

} else {

incr = -incr;

}

aet[i].x += incr;

}

What is wrong in this calculation?

You don't show the scanline algorithm proper, but is looks as if you wanted to treat all scanlines from `Ymin`

to `Ymax`

. `y`

starts a `Ymin`

and increases by one for each scanline.

Therefore, the `x`

for each scanline should increase by `dx/dy`

.

You probably don't need to adjust the sign. Instead, the sign of `dy`

is either positive or negative:

```
m = dx / dy # slope of the line
x[y + 1] = x[y] + m * dy = x[y] + m # dy == (y + 1) - y == 1
x[y - 1] = x[y] + m * dy = x[y] - m # dy == (y - 1) - y == -1
```

You scan in `x`

direction and hence rule out horizontal lines for which `dy == 0`

. That also shows in your maths: You cannot divide by `dy`

when `dy == 0`

.

您可能感兴趣的文章：

- android - Java InputSream hascode is different each time even when the request is same
- cq5 - Adobe CQ / AEM Informations
- c# - Can I get json response without using ajax call in jquery
- WSO2 Authentication Failed
- python 3.x - Passing arguments to argparse from subprocess.Popen
- php - Reading a specific line from a text file
- clr - Any implementation of an Unrolled Linked List in C#?
- Finding Hudson Log Files
- Forward to a payment-gateway together with POST data using cURL (or any other PHP server side solution)
- WCF in Winforms app - is it always single-threaded?

随机阅读：

**推荐内容**-

**热点内容**-
- php - Reading a specific line from a text file
- clr - Any implementation of an Unrolled Linked List in C#?
- Finding Hudson Log Files
- Forward to a payment-gateway together with POST data using cURL (or any other PHP server side solution)
- WCF in Winforms app - is it always single-threaded?
- git svn - git svn fetch does not fetch a Subversion commit message modified after initial clone
- java me - Why I am getting the bad length exception when I am running this application?
- java - How to get string.format to complain at compile time
- ruby on rails - Trigger observer of parent class on change
- python - Issue with URL pattern in Django with webmonkey tutorial