Updated readability of the code.
[sixth-3d.git] / src / main / java / eu / svjatoslav / sixth / e3d / renderer / raster / shapes / basic / texturedpolygon / PolygonBorderInterpolator.java
1 /*
2  * Sixth 3D engine. Author: Svjatoslav Agejenko.
3  * This project is released under Creative Commons Zero (CC0) license.
4  */
5 package eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.texturedpolygon;
6
7 import eu.svjatoslav.sixth.e3d.geometry.Point2D;
8
9 import static java.lang.Math.abs;
10
11 public class PolygonBorderInterpolator implements
12         Comparable<PolygonBorderInterpolator> {
13
14     // on-screen coordinates
15     Point2D onScreenPoint1;
16     Point2D onScreenPoint2;
17
18     double distanceFromY1;
19     private int onScreenHeight;
20     private int onScreenWidth;
21     private int onscreenAbsoluteHeight;
22     private double textureWidth;
23     private double textureHeight;
24     // texture coordinates
25     private Point2D texturePoint1;
26     private Point2D texturePoint2;
27
28
29     @Override
30     public boolean equals(final Object o) {
31         if (o == null) return false;
32
33         return o instanceof PolygonBorderInterpolator && compareTo((PolygonBorderInterpolator) o) == 0;
34     }
35
36     @Override
37     public int hashCode() {
38         int result = onScreenWidth;
39         result = 31 * result + onscreenAbsoluteHeight;
40         return result;
41     }
42
43     @Override
44     public int compareTo(final PolygonBorderInterpolator otherInterpolator) {
45         if (onscreenAbsoluteHeight < otherInterpolator.onscreenAbsoluteHeight)
46             return 1;
47         if (onscreenAbsoluteHeight > otherInterpolator.onscreenAbsoluteHeight)
48             return -1;
49
50         if (onScreenWidth < otherInterpolator.onScreenWidth)
51             return 1;
52         if (onScreenWidth > otherInterpolator.onScreenWidth)
53             return -1;
54
55         return 0;
56     }
57
58     public boolean containsY(final int y) {
59
60         if (onScreenPoint1.y < onScreenPoint2.y) {
61             if (y >= onScreenPoint1.y)
62                 return y <= onScreenPoint2.y;
63         } else if (y >= onScreenPoint2.y)
64             return y <= onScreenPoint1.y;
65
66         return false;
67     }
68
69     public double getTX() {
70
71         if (onScreenHeight == 0)
72             return (texturePoint2.x + texturePoint1.x) / 2d;
73
74         return texturePoint1.x + ((textureWidth * distanceFromY1) / onScreenHeight);
75     }
76
77     public double getTY() {
78
79         if (onScreenHeight == 0)
80             return (texturePoint2.y + texturePoint1.y) / 2d;
81
82         return texturePoint1.y + ((textureHeight * distanceFromY1) / onScreenHeight);
83     }
84
85     public int getX() {
86
87         if (onScreenHeight == 0)
88             return (int) ((onScreenPoint2.x + onScreenPoint1.x) / 2d);
89
90         return (int) (onScreenPoint1.x + ((onScreenWidth * distanceFromY1) / onScreenHeight));
91     }
92
93     public void setCurrentY(final int y) {
94         distanceFromY1 = y - onScreenPoint1.y;
95     }
96
97     public void setPoints(final Point2D onScreenPoint1, final Point2D onScreenPoint2,
98                           final Point2D texturePoint1, final Point2D texturePoint2) {
99
100         this.onScreenPoint1 = onScreenPoint1;
101         this.onScreenPoint2 = onScreenPoint2;
102         this.texturePoint1 = texturePoint1;
103         this.texturePoint2 = texturePoint2;
104
105         onScreenHeight = (int) (onScreenPoint2.y - onScreenPoint1.y);
106         onScreenWidth = (int) (onScreenPoint2.x - onScreenPoint1.x);
107         onscreenAbsoluteHeight = abs(onScreenHeight);
108
109         textureWidth = texturePoint2.x - texturePoint1.x;
110         textureHeight = texturePoint2.y - texturePoint1.y;
111     }
112
113 }