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

Documentation

TODO:DESCRIPTION

C++

emphasis.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
(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)