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