1/*
2
3BezierBounds
4
5Copyright (c) 2002 Haiku.
6
7Author:
8	Michael Pfeiffer
9
10Permission is hereby granted, free of charge, to any person obtaining a copy of
11this software and associated documentation files (the "Software"), to deal in
12the Software without restriction, including without limitation the rights to
13use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
14of the Software, and to permit persons to whom the Software is furnished to do
15so, subject to the following conditions:
16
17The above copyright notice and this permission notice shall be included in all
18copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26THE SOFTWARE.
27
28*/
29
30
31#include "BezierBounds.h"
32#include <float.h>
33#include <math.h>
34
35BRect BezierBounds(BPoint* points, int numOfPoints)
36{
37	BRect bounds(FLT_MAX, FLT_MAX, -FLT_MIN, -FLT_MIN);
38	for (int i = 0; i < numOfPoints; i ++, points ++) {
39		bounds.left   = min_c(bounds.left,   points->x);
40		bounds.right  = max_c(bounds.right,  points->x);
41		bounds.top    = min_c(bounds.top,    points->y);
42		bounds.bottom = max_c(bounds.bottom, points->y);
43	}
44	return bounds;
45}
46
47static float PenSizeCorrection(float penSize, cap_mode capMode, join_mode joinMode, float miterLimit)
48{
49	const float halfPenSize = penSize / 2.0;
50	float correction = 0.0;
51
52	switch (capMode) {
53		case B_ROUND_CAP:
54		case B_BUTT_CAP:
55			correction = halfPenSize;
56			break;
57		case B_SQUARE_CAP:
58			correction = M_SQRT2 * halfPenSize;
59			break;
60	}
61
62	switch (joinMode) {
63		case B_ROUND_JOIN:
64		case B_BEVEL_JOIN:
65		case B_BUTT_JOIN:
66			correction = max_c(correction, halfPenSize);
67			break;
68		case B_MITER_JOIN:
69			correction = max_c(correction, halfPenSize * miterLimit);
70			break;
71		case B_SQUARE_JOIN:
72			correction = max_c(correction, M_SQRT2 * halfPenSize);
73			break;
74	}
75
76	return correction;
77}
78
79BRect BezierBounds(BPoint* points, int numOfPoints, float penSize, cap_mode capMode, join_mode joinMode, float miterLimit)
80{
81	BRect bounds = BezierBounds(points, numOfPoints);
82	float w = -PenSizeCorrection(penSize, capMode, joinMode, miterLimit);
83	bounds.InsetBy(w, w);
84	return bounds;
85}
86
87
88