1300e3a75063113b620df1797caa32e4e9848cee
[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 public class PolygonBorderInterpolator implements
10         Comparable<PolygonBorderInterpolator> {
11
12     // on-screen coordinates
13     Point2D p1;
14     Point2D p2;
15     double distanceFromY1;
16     private int height;
17     private int width;
18     private int absoluteHeight;
19     private double twidth;
20     private double theight;
21     // texture coordinates
22     private Point2D tp1;
23     private Point2D tp2;
24
25
26     @Override
27     public boolean equals(final Object o) {
28         if (o == null) return false;
29
30         return o instanceof PolygonBorderInterpolator && compareTo((PolygonBorderInterpolator) o) == 0;
31     }
32
33     @Override
34     public int hashCode() {
35         int result = width;
36         result = 31 * result + absoluteHeight;
37         return result;
38     }
39
40     @Override
41     public int compareTo(final PolygonBorderInterpolator o) {
42         if (absoluteHeight < o.absoluteHeight)
43             return 1;
44         if (absoluteHeight > o.absoluteHeight)
45             return -1;
46
47         if (width < o.width)
48             return 1;
49         if (width > o.width)
50             return -1;
51
52         return 0;
53     }
54
55     public boolean containsY(final int y) {
56
57         if (p1.y < p2.y) {
58             if (y >= p1.y)
59                 if (y <= p2.y)
60                     return true;
61         } else if (y >= p2.y)
62             if (y <= p1.y)
63                 return true;
64
65         return false;
66     }
67
68     public double getTX() {
69
70         if (height == 0)
71             return (tp2.x + tp1.x) / 2d;
72
73         return tp1.x + ((twidth * distanceFromY1) / height);
74     }
75
76     public double getTY() {
77
78         if (height == 0)
79             return (tp2.y + tp1.y) / 2d;
80
81         return tp1.y + ((theight * distanceFromY1) / height);
82     }
83
84     public int getX() {
85
86         if (height == 0)
87             return (int) ((p2.x + p1.x) / 2d);
88
89         return (int) (p1.x + ((width * distanceFromY1) / height));
90     }
91
92     public void setCurrentY(final int y) {
93         distanceFromY1 = y - p1.y;
94     }
95
96     public void setPoints(final Point2D p1, final Point2D p2,
97                           final Point2D tp1, final Point2D tp2) {
98
99         this.p1 = p1;
100         this.p2 = p2;
101         this.tp1 = tp1;
102         this.tp2 = tp2;
103
104         height = (int) (p2.y - p1.y);
105         width = (int) (p2.x - p1.x);
106         absoluteHeight = Math.abs(height);
107
108         twidth = tp2.x - tp1.x;
109         theight = tp2.y - tp1.y;
110     }
111
112 }