// Copyright (c) 2017 Kiel University and others.
// Copyright (c) 2026 The elk-go Authors.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0.
//
// SPDX-License-Identifier: EPL-2.0

package spore

import 

type timestamp struct {
	node *node
	low  bool
}

func scanlineOverlaps( []*node) []*tEdge {
	 := make([]timestamp, 0, len()*2)
	for ,  := range  {
		 = append(, timestamp{node: , low: true}, timestamp{node: })
	}
	sort.SliceStable(, func(,  int) bool {
		,  := [], []
		 := .node.rect.Y
		if !.low {
			 += .node.rect.Height
		}
		 := .node.rect.Y
		if !.low {
			 += .node.rect.Height
		}
		if  := javaDoubleCompare(, );  != 0 {
			return  < 0
		}
		// The 0.8.1 comparator deliberately processes an ending border before
		// a beginning border at the same coordinate.
		return !.low && .low
	})

	 := make([]*node, 0)
	 := make([]*tEdge, 0)
	for ,  := range  {
		if !.low {
			for ,  := range  {
				if  == .node {
					 = append([:], [+1:]...)
					break
				}
			}
			continue
		}

		 := sort.Search(len(), func( int) bool {
			return compareScanlineNodes([], .node) >= 0
		})
		 = append(, nil)
		copy([+1:], [:])
		[] = .node
		 := false
		for ,  := range  {
			if scanlineOverlap(.node, ) {
				 = addEdge(, &tEdge{u: .node.original, v: .original})
				 = true
			} else if  {
				break
			}
		}
	}
	return 
}

func compareScanlineNodes(,  *node) int {
	if  := javaDoubleCompare(.rect.X, .rect.X);  != 0 {
		return 
	}
	if  := javaDoubleCompare(.original.point.X, .original.point.X);  != 0 {
		return 
	}
	return javaDoubleCompare(.original.point.Y, .original.point.Y)
}

func scanlineOverlap(,  *node) bool {
	if  == nil ||  == nil ||  ==  {
		return false
	}
	return fuzzyCompare(.rect.X, .rect.X+.rect.Width, fuzziness) < 0 &&
		fuzzyCompare(.rect.X, .rect.X+.rect.Width, fuzziness) < 0
}