Skip to content

KDNode and nearest #1

@kaitlynhwang

Description

@kaitlynhwang

I'm watching your video on Safari. I read your README regarding the KDNode and nearest. Would you give an example of points where lines 99 to 118 is necessary? I tried the following as a test between your code and the revised version of nearest below. Both gave the same output.

kd = KDTree()
kd.add([2,3])
kd.add([3,4])
kd.add([5,5])
kd.add([9,6])
kd.add([4,7])
kd.add([8,1])
kd.add([7,2])
v = kd.nearest((4,4))
print(v.point)

And simplify the function to

  def nearest(self, min_distance, p):
        """Return closest node from given subtree to point."""

        d = self.distance(p)
        result = None
        if (d < min_distance):
            result = self
            min_distance = d

        pt = None
        if self.isAbove(p) and self.above:
            pt = self.above.nearest(min_distance, p)
        elif self.isBelow(p) and self.below:
            pt = self.below.nearest(min_distance, p)
        if pt:
            return pt

        return result

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions