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