mario::konrad
programming / C++ / sailing / nerd stuff
Algorithm: Emphasis
© 2005 / Mario Konrad

Documentation

TODO:DESCRIPTION

C++

emphasis.cpp:

#include <iostream>
#include <vector>
#include <algorithm>

typedef std::pair<double, double> Point;
typedef std::vector<Point> Polygon;

Point emphasis(const Polygon & polygon)
{
    if (polygon.size() < 3) return Point(0.0, 0.0);
    Point a(0.0, 0.0);
    std::for_each (polygon.begin(), polygon.end(), [&a](const Point & p)
    {
        a.first  += p.first;
        a.second += p.second;
    });
    return Point(a.first / polygon.size(), a.second / polygon.size());
}

int main(int, char **)
{
    Polygon polygon =
    {
        Point(5,  5),
        Point(7,  5),
        Point(8, 10),
        Point(5, 12),
        Point(3,  7),
    };

    Point e = emphasis(polygon);
    std::cout << "emphasis of non-selfintersection polygon: ("
        << e.first << ","
        << e.second << ")"
        << std::endl;

    return 0;
}

Scheme

emphasis.scm:

(define (vec-add v)
    (cond
        ((null? v) '(0 0))
        ((not (list? v)) #f)
        (else
            (let ((x (car v)) (y (vec-add (cdr v))))
                (list
                    (+ (car x) (car y))
                    (+ (cadr x) (cadr y)))))))

(define (calc-emphasis v)
    (cond
        ((null? v) 0)
        ((= (length v) 0) 0)
        (else
            (let ((x (vec-add v)) (l (length v)))
                (map (lambda (s) (/ s l)) x)))))

(define polygon '((5  5) (7  5) (8 10) (5 12) (3  7)))

(display "emphasis of ")
(display polygon)
(display " = ")
(display (calc-emphasis polygon))
(newline)

(exit)