Changed license to Creative Commons Zero (CC0).
[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 *
6  */
7
8 package eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.texturedpolygon;
9
10 import eu.svjatoslav.sixth.e3d.geometry.Point2D;
11
12 public class PolygonBorderInterpolator implements
13         Comparable<PolygonBorderInterpolator> {
14
15     // on-screen coordinates
16     Point2D p1;
17     Point2D p2;
18     double distanceFromY1;
19     private int height;
20     private int width;
21     private int absoluteHeight;
22     private double twidth;
23     private double theight;
24     // texture coordinates
25     private Point2D tp1;
26     private Point2D tp2;
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 = width;
39         result = 31 * result + absoluteHeight;
40         return result;
41     }
42
43     @Override
44     public int compareTo(final PolygonBorderInterpolator o) {
45         if (absoluteHeight < o.absoluteHeight)
46             return 1;
47         if (absoluteHeight > o.absoluteHeight)
48             return -1;
49
50         if (width < o.width)
51             return 1;
52         if (width > o.width)
53             return -1;
54
55         return 0;
56     }
57
58     public boolean containsY(final int y) {
59
60         if (p1.y < p2.y) {
61             if (y >= p1.y)
62                 if (y <= p2.y)
63                     return true;
64         } else if (y >= p2.y)
65             if (y <= p1.y)
66                 return true;
67
68         return false;
69     }
70
71     public double getTX() {
72
73         if (height == 0)
74             return (tp2.x + tp1.x) / 2d;
75
76         return tp1.x + ((twidth * distanceFromY1) / height);
77     }
78
79     public double getTY() {
80
81         if (height == 0)
82             return (tp2.y + tp1.y) / 2d;
83
84         return tp1.y + ((theight * distanceFromY1) / height);
85     }
86
87     public int getX() {
88
89         if (height == 0)
90             return (int) ((p2.x + p1.x) / 2d);
91
92         return (int) (p1.x + ((width * distanceFromY1) / height));
93     }
94
95     public void setCurrentY(final int y) {
96         distanceFromY1 = y - p1.y;
97     }
98
99     public void setPoints(final Point2D p1, final Point2D p2,
100                           final Point2D tp1, final Point2D tp2) {
101
102         this.p1 = p1;
103         this.p2 = p2;
104         this.tp1 = tp1;
105         this.tp2 = tp2;
106
107         height = (int) (p2.y - p1.y);
108         width = (int) (p2.x - p1.x);
109         absoluteHeight = Math.abs(height);
110
111         twidth = tp2.x - tp1.x;
112         theight = tp2.y - tp1.y;
113     }
114
115 }