Sunday, July 17, 2016

if points are vertically symmetric

The vertical line of symmetry is at average of x for all points (assuming points have no dupes). If a mirrored point exists within points for all points, then the points are vertically symmetric.

note: consider only distinct points as dupes skew xavg.
bool (points):
    if points == null:
        throw
    result = true
    distinctpoints = points.distinct()
    // xavg using distinct as dupes skew avg
    xavg = distinctpoints.avg(p => p.x)
    for p in distinctpoints:
        if !distinctpoints.has(mirror(p, xavg))
            result = false
            break
    return result

mirror(p, xavg):
    return point(xavg + (xavg-p.x), p.y)

No comments:

Post a Comment