2 * Sixth 3D engine. Author: Svjatoslav Agejenko.
3 * This project is released under Creative Commons Zero (CC0) license.
5 package eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.texturedpolygon;
7 import eu.svjatoslav.sixth.e3d.geometry.Point2D;
9 import static java.lang.Math.abs;
11 public class PolygonBorderInterpolator implements
12 Comparable<PolygonBorderInterpolator> {
14 // on-screen coordinates
15 Point2D onScreenPoint1;
16 Point2D onScreenPoint2;
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;
30 public boolean equals(final Object o) {
31 if (o == null) return false;
33 return o instanceof PolygonBorderInterpolator && compareTo((PolygonBorderInterpolator) o) == 0;
37 public int hashCode() {
38 int result = onScreenWidth;
39 result = 31 * result + onscreenAbsoluteHeight;
44 public int compareTo(final PolygonBorderInterpolator otherInterpolator) {
45 if (onscreenAbsoluteHeight < otherInterpolator.onscreenAbsoluteHeight)
47 if (onscreenAbsoluteHeight > otherInterpolator.onscreenAbsoluteHeight)
50 if (onScreenWidth < otherInterpolator.onScreenWidth)
52 if (onScreenWidth > otherInterpolator.onScreenWidth)
58 public boolean containsY(final int y) {
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;
69 public double getTX() {
71 if (onScreenHeight == 0)
72 return (texturePoint2.x + texturePoint1.x) / 2d;
74 return texturePoint1.x + ((textureWidth * distanceFromY1) / onScreenHeight);
77 public double getTY() {
79 if (onScreenHeight == 0)
80 return (texturePoint2.y + texturePoint1.y) / 2d;
82 return texturePoint1.y + ((textureHeight * distanceFromY1) / onScreenHeight);
87 if (onScreenHeight == 0)
88 return (int) ((onScreenPoint2.x + onScreenPoint1.x) / 2d);
90 return (int) (onScreenPoint1.x + ((onScreenWidth * distanceFromY1) / onScreenHeight));
93 public void setCurrentY(final int y) {
94 distanceFromY1 = y - onScreenPoint1.y;
97 public void setPoints(final Point2D onScreenPoint1, final Point2D onScreenPoint2,
98 final Point2D texturePoint1, final Point2D texturePoint2) {
100 this.onScreenPoint1 = onScreenPoint1;
101 this.onScreenPoint2 = onScreenPoint2;
102 this.texturePoint1 = texturePoint1;
103 this.texturePoint2 = texturePoint2;
105 onScreenHeight = (int) (onScreenPoint2.y - onScreenPoint1.y);
106 onScreenWidth = (int) (onScreenPoint2.x - onScreenPoint1.x);
107 onscreenAbsoluteHeight = abs(onScreenHeight);
109 textureWidth = texturePoint2.x - texturePoint1.x;
110 textureHeight = texturePoint2.y - texturePoint1.y;