1c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy/*
2c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy * Copyright 2018 The Android Open Source Project
3c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy *
4c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy * Licensed under the Apache License, Version 2.0 (the "License");
5c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy * you may not use this file except in compliance with the License.
6c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy * You may obtain a copy of the License at
7c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy *
8c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy *      http://www.apache.org/licenses/LICENSE-2.0
9c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy *
10c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy * Unless required by applicable law or agreed to in writing, software
11c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy * distributed under the License is distributed on an "AS IS" BASIS,
12c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy * See the License for the specific language governing permissions and
14c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy * limitations under the License.
15c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy */
16c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy
17c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guypackage androidx.core.graphics;
18c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy
19c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guyimport static androidx.core.util.Preconditions.checkNotNull;
20c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy
21c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guyimport android.graphics.Path;
22c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guyimport android.graphics.PointF;
23c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy
24c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guyimport androidx.annotation.NonNull;
25c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy
26c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy/**
27c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy * A line segment that represents an approximate fraction of a {@link Path} after
28c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy * {@linkplain PathUtils#flatten(Path) flattening}.
29c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy */
30c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guypublic final class PathSegment {
31c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy    private final PointF mStart;
32c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy    private final float mStartFraction;
33c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy    private final PointF mEnd;
34c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy    private final float mEndFraction;
35c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy
36c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy    public PathSegment(@NonNull PointF start, float startFraction, @NonNull PointF end,
37c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy            float endFraction) {
38c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy        mStart = checkNotNull(start, "start == null");
39c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy        mStartFraction = startFraction;
40c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy        mEnd = checkNotNull(end, "end == null");
41c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy        mEndFraction = endFraction;
42c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy    }
43c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy
44c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy    /** The start point of the line segment. */
45c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy    @NonNull
46c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy    public PointF getStart() {
47c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy        return mStart;
48c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy    }
49c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy
50c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy    /**
51c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy     * Fraction along the length of the path that the {@linkplain #getStart() start point} resides.
52c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy     */
53c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy    public float getStartFraction() {
54c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy        return mStartFraction;
55c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy    }
56c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy
57c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy    /** The end point of the line segment. */
58c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy    @NonNull
59c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy    public PointF getEnd() {
60c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy        return mEnd;
61c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy    }
62c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy
63c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy    /**
64c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy     * Fraction along the length of the path that the {@linkplain #getEnd() end point} resides.
65c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy     */
66c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy    public float getEndFraction() {
67c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy        return mEndFraction;
68c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy    }
69c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy
70c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy    @Override
71c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy    public boolean equals(Object o) {
72c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy        if (this == o) return true;
73c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy        if (!(o instanceof PathSegment)) return false;
74c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy        PathSegment that = (PathSegment) o;
75c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy        return Float.compare(mStartFraction, that.mStartFraction) == 0
76c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy                && Float.compare(mEndFraction, that.mEndFraction) == 0
77c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy                && mStart.equals(that.mStart)
78c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy                && mEnd.equals(that.mEnd);
79c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy    }
80c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy
81c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy    @Override
82c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy    public int hashCode() {
83c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy        int result = mStart.hashCode();
84c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy        result = 31 * result + (mStartFraction != +0.0f ? Float.floatToIntBits(mStartFraction) : 0);
85c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy        result = 31 * result + mEnd.hashCode();
86c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy        result = 31 * result + (mEndFraction != +0.0f ? Float.floatToIntBits(mEndFraction) : 0);
87c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy        return result;
88c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy    }
89c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy
90c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy    @Override
91c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy    public String toString() {
92c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy        return "PathSegment{"
93c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy                + "start=" + mStart
94c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy                + ", startFraction=" + mStartFraction
95c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy                + ", end=" + mEnd
96c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy                + ", endFraction=" + mEndFraction
97c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy                + '}';
98c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy    }
99c5032d73c8d389a2b62cc721bfecde2937d4a363Romain Guy}
100