c0ae36f7108fe35f659caa7dd44d0caad15a1aa4
[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                 return y <= p2.y;
60         } else if (y >= p2.y)
61             return y <= p1.y;
62
63         return false;
64     }
65
66     public double getTX() {
67
68         if (height == 0)
69             return (tp2.x + tp1.x) / 2d;
70
71         return tp1.x + ((twidth * distanceFromY1) / height);
72     }
73
74     public double getTY() {
75
76         if (height == 0)
77             return (tp2.y + tp1.y) / 2d;
78
79         return tp1.y + ((theight * distanceFromY1) / height);
80     }
81
82     public int getX() {
83
84         if (height == 0)
85             return (int) ((p2.x + p1.x) / 2d);
86
87         return (int) (p1.x + ((width * distanceFromY1) / height));
88     }
89
90     public void setCurrentY(final int y) {
91         distanceFromY1 = y - p1.y;
92     }
93
94     public void setPoints(final Point2D p1, final Point2D p2,
95                           final Point2D tp1, final Point2D tp2) {
96
97         this.p1 = p1;
98         this.p2 = p2;
99         this.tp1 = tp1;
100         this.tp2 = tp2;
101
102         height = (int) (p2.y - p1.y);
103         width = (int) (p2.x - p1.x);
104         absoluteHeight = Math.abs(height);
105
106         twidth = tp2.x - tp1.x;
107         theight = tp2.y - tp1.y;
108     }
109
110 }