152a38012Sejakowatz /*
252a38012Sejakowatz
352a38012Sejakowatz BezierBounds
452a38012Sejakowatz
5*2ca13760SColdfirex Copyright (c) 2002 Haiku.
652a38012Sejakowatz
752a38012Sejakowatz Author:
852a38012Sejakowatz Michael Pfeiffer
952a38012Sejakowatz
1052a38012Sejakowatz Permission is hereby granted, free of charge, to any person obtaining a copy of
1152a38012Sejakowatz this software and associated documentation files (the "Software"), to deal in
1252a38012Sejakowatz the Software without restriction, including without limitation the rights to
1352a38012Sejakowatz use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
1452a38012Sejakowatz of the Software, and to permit persons to whom the Software is furnished to do
1552a38012Sejakowatz so, subject to the following conditions:
1652a38012Sejakowatz
1752a38012Sejakowatz The above copyright notice and this permission notice shall be included in all
1852a38012Sejakowatz copies or substantial portions of the Software.
1952a38012Sejakowatz
2052a38012Sejakowatz THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2152a38012Sejakowatz IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2252a38012Sejakowatz FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
2352a38012Sejakowatz AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2452a38012Sejakowatz LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2552a38012Sejakowatz OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2652a38012Sejakowatz THE SOFTWARE.
2752a38012Sejakowatz
2852a38012Sejakowatz */
2952a38012Sejakowatz
3052a38012Sejakowatz
3152a38012Sejakowatz #include "BezierBounds.h"
3252a38012Sejakowatz #include <float.h>
3352a38012Sejakowatz #include <math.h>
3452a38012Sejakowatz
BezierBounds(BPoint * points,int numOfPoints)3552a38012Sejakowatz BRect BezierBounds(BPoint* points, int numOfPoints)
3652a38012Sejakowatz {
3752a38012Sejakowatz BRect bounds(FLT_MAX, FLT_MAX, -FLT_MIN, -FLT_MIN);
3852a38012Sejakowatz for (int i = 0; i < numOfPoints; i ++, points ++) {
3952a38012Sejakowatz bounds.left = min_c(bounds.left, points->x);
4052a38012Sejakowatz bounds.right = max_c(bounds.right, points->x);
4152a38012Sejakowatz bounds.top = min_c(bounds.top, points->y);
4252a38012Sejakowatz bounds.bottom = max_c(bounds.bottom, points->y);
4352a38012Sejakowatz }
4452a38012Sejakowatz return bounds;
4552a38012Sejakowatz }
4652a38012Sejakowatz
PenSizeCorrection(float penSize,cap_mode capMode,join_mode joinMode,float miterLimit)4752a38012Sejakowatz static float PenSizeCorrection(float penSize, cap_mode capMode, join_mode joinMode, float miterLimit)
4852a38012Sejakowatz {
4952a38012Sejakowatz const float halfPenSize = penSize / 2.0;
5052a38012Sejakowatz float correction = 0.0;
5152a38012Sejakowatz
5252a38012Sejakowatz switch (capMode) {
5352a38012Sejakowatz case B_ROUND_CAP:
5452a38012Sejakowatz case B_BUTT_CAP:
5552a38012Sejakowatz correction = halfPenSize;
5652a38012Sejakowatz break;
5752a38012Sejakowatz case B_SQUARE_CAP:
5852a38012Sejakowatz correction = M_SQRT2 * halfPenSize;
5952a38012Sejakowatz break;
6052a38012Sejakowatz }
6152a38012Sejakowatz
6252a38012Sejakowatz switch (joinMode) {
6352a38012Sejakowatz case B_ROUND_JOIN:
6452a38012Sejakowatz case B_BEVEL_JOIN:
6552a38012Sejakowatz case B_BUTT_JOIN:
6652a38012Sejakowatz correction = max_c(correction, halfPenSize);
6752a38012Sejakowatz break;
6852a38012Sejakowatz case B_MITER_JOIN:
6952a38012Sejakowatz correction = max_c(correction, halfPenSize * miterLimit);
7052a38012Sejakowatz break;
7152a38012Sejakowatz case B_SQUARE_JOIN:
7252a38012Sejakowatz correction = max_c(correction, M_SQRT2 * halfPenSize);
7352a38012Sejakowatz break;
7452a38012Sejakowatz }
7552a38012Sejakowatz
7652a38012Sejakowatz return correction;
7752a38012Sejakowatz }
7852a38012Sejakowatz
BezierBounds(BPoint * points,int numOfPoints,float penSize,cap_mode capMode,join_mode joinMode,float miterLimit)7952a38012Sejakowatz BRect BezierBounds(BPoint* points, int numOfPoints, float penSize, cap_mode capMode, join_mode joinMode, float miterLimit)
8052a38012Sejakowatz {
8152a38012Sejakowatz BRect bounds = BezierBounds(points, numOfPoints);
8252a38012Sejakowatz float w = -PenSizeCorrection(penSize, capMode, joinMode, miterLimit);
8352a38012Sejakowatz bounds.InsetBy(w, w);
8452a38012Sejakowatz return bounds;
8552a38012Sejakowatz }
8652a38012Sejakowatz
8752a38012Sejakowatz
88