Code cleanup and formatting.
[sixth-3d.git] / src / main / java / eu / svjatoslav / sixth / e3d / renderer / raster / shapes / basic / solidpolygon / LineInterpolator.java
1 /*
2  * Sixth 3D engine. Copyright ©2012-2018, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of version 3 of the GNU Lesser General Public License
6  * or later as published by the Free Software Foundation.
7  *
8  */
9
10 package eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.solidpolygon;
11
12 import eu.svjatoslav.sixth.e3d.geometry.Point2D;
13
14 public class LineInterpolator implements Comparable<LineInterpolator> {
15
16     Point2D p1;
17     Point2D p2;
18     private int height;
19     private int width;
20     private int absoluteHeight;
21
22     @Override
23     public boolean equals(final Object o) {
24         if (o == null) return false;
25
26         return o instanceof LineInterpolator && compareTo((LineInterpolator) o) == 0;
27     }
28
29     @Override
30     public int compareTo(final LineInterpolator o) {
31         if (absoluteHeight < o.absoluteHeight)
32             return 1;
33         if (absoluteHeight > o.absoluteHeight)
34             return -1;
35
36         return Integer.compare(o.width, width);
37
38     }
39
40     @Override
41     public int hashCode() {
42         int result = width;
43         result = 31 * result + absoluteHeight;
44         return result;
45     }
46
47     public boolean containsY(final int y) {
48
49         if (p1.y <= p2.y) {
50             if (y >= p1.y)
51                 if (y <= p2.y)
52                     return true;
53         } else if (y >= p2.y)
54             if (y <= p1.y)
55                 return true;
56
57         return false;
58     }
59
60     public int getX(final int y) {
61
62         if (height == 0)
63             return (int) (p2.x + p1.x) / 2;
64
65         return (int) (p1.x + ((width * (y - p1.y)) / height));
66     }
67
68     public void setPoints(final Point2D p1, final Point2D p2) {
69         this.p1 = p1;
70         this.p2 = p2;
71         height = (int) (p2.y - p1.y);
72         width = (int) (p2.x - p1.x);
73
74         absoluteHeight = Math.abs(height);
75     }
76
77 }