1/*
2 * Copyright (C) 2014 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "GeometryUtilities.h"
28
29namespace WebCore {
30
31float findSlope(const FloatPoint& p1, const FloatPoint& p2, float& c)
32{
33    if (p2.x() == p1.x())
34        return std::numeric_limits<float>::infinity();
35
36    // y = mx + c
37    float slope = (p2.y() - p1.y()) / (p2.x() - p1.x());
38    c = p1.y() - slope * p1.x();
39    return slope;
40}
41
42bool findIntersection(const FloatPoint& p1, const FloatPoint& p2, const FloatPoint& d1, const FloatPoint& d2, FloatPoint& intersection)
43{
44    float pOffset = 0;
45    float pSlope = findSlope(p1, p2, pOffset);
46
47    float dOffset = 0;
48    float dSlope = findSlope(d1, d2, dOffset);
49
50    if (dSlope == pSlope)
51        return false;
52
53    if (pSlope == std::numeric_limits<float>::infinity()) {
54        intersection.setX(p1.x());
55        intersection.setY(dSlope * intersection.x() + dOffset);
56        return true;
57    }
58    if (dSlope == std::numeric_limits<float>::infinity()) {
59        intersection.setX(d1.x());
60        intersection.setY(pSlope * intersection.x() + pOffset);
61        return true;
62    }
63
64    // Find x at intersection, where ys overlap; x = (c' - c) / (m - m')
65    intersection.setX((dOffset - pOffset) / (pSlope - dSlope));
66    intersection.setY(pSlope * intersection.x() + pOffset);
67    return true;
68}
69
70IntRect unionRect(const Vector<IntRect>& rects)
71{
72    IntRect result;
73
74    size_t count = rects.size();
75    for (size_t i = 0; i < count; ++i)
76        result.unite(rects[i]);
77
78    return result;
79}
80
81FloatRect unionRect(const Vector<FloatRect>& rects)
82{
83    FloatRect result;
84
85    size_t count = rects.size();
86    for (size_t i = 0; i < count; ++i)
87        result.unite(rects[i]);
88
89    return result;
90}
91
92FloatRect mapRect(const FloatRect& r, const FloatRect& srcRect, const FloatRect& destRect)
93{
94    if (!srcRect.width() || !srcRect.height())
95        return FloatRect();
96
97    float widthScale = destRect.width() / srcRect.width();
98    float heightScale = destRect.height() / srcRect.height();
99    return FloatRect(destRect.x() + (r.x() - srcRect.x()) * widthScale,
100        destRect.y() + (r.y() - srcRect.y()) * heightScale,
101        r.width() * widthScale, r.height() * heightScale);
102}
103
104FloatRect largestRectWithAspectRatioInsideRect(float aspectRatio, const FloatRect& srcRect)
105{
106    FloatRect destRect = srcRect;
107
108    if (aspectRatio > srcRect.size().aspectRatio()) {
109        float dy = destRect.width() / aspectRatio - destRect.height();
110        destRect.inflateY(dy / 2);
111    } else {
112        float dx = destRect.height() * aspectRatio - destRect.width();
113        destRect.inflateX(dx / 2);
114    }
115    return destRect;
116}
117
118}
119