public function add(Edge $edge) 
    {
        $dwinding = $edge->y0 < $edge->y1 ? 1 : -1;
        
        $layer = $this->topLayer;
        $previousLayer = null;
        
        while ($layer !== null) {
            if ($layer->polygonId === $edge->polygonId) {
                $layer->winding += $dwinding;

                $inPath = $layer->windingRule == 'evenodd' ? 
                    ($layer->winding % 2 === 1) : ($layer->winding !== 0);

                if (!$inPath) {
                    // Remove layer
                    if ($previousLayer === null) {
                        $this->topLayer = $layer->nextLayer;
                    }
                    else {
                        $previousLayer->nextLayer = $layer->nextLayer;
                    }
                }
                break;
            } elseif ($layer->polygonId < $edge->polygonId) {
                // Insert here
                $newLayer = new Layer(
                    $edge->polygonId,
                    $edge->color,
                    $dwinding,
                    $edge->windingRule
                );
                $newLayer->nextLayer = $layer;
                
                if ($previousLayer === null) {
                    $this->topLayer = $newLayer;
                } else {
                    $previousLayer->nextLayer = $newLayer;
                }
                break;
            }
            
            $previousLayer = $layer;
            $layer = $layer->nextLayer;
        }
        
        if ($layer === null) {
            $newLayer = new Layer(
                $edge->polygonId,
                $edge->color,
                $dwinding,
                $edge->windingRule
            );
            
            if ($previousLayer === null) {
                $this->topLayer = $newLayer;
            } else {
                $previousLayer->nextLayer = $newLayer;
            }
        }

        // Update current color
        $color = ColorUtils::TRANSPARENT;
        $layer = $this->topLayer;
        
        while ($layer !== null && ($color & 0xff) < 255) {
            if ($layer->color === ColorUtils::FORCE_TRANSPARENT) {
                break;
            }
            
            $color = ColorUtils::over($layer->color, $color);
        }
        
        $this->color = $color;
    }