Formatting update
[sixth-3d.git] / src / main / java / eu / svjatoslav / sixth / e3d / renderer / raster / shapes / basic / line / LineInterpolator.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.line;
6
7 public class LineInterpolator {
8
9     private double x1, y1, d1, x2, y2, d2;
10
11     private double d;
12     private int height;
13     private int width;
14     private double dinc;
15
16     public boolean containsY(final int y) {
17
18         if (y1 < y2) {
19             if (y >= y1)
20                 if (y <= y2)
21                     return true;
22         } else if (y >= y2)
23             if (y <= y1)
24                 return true;
25
26         return false;
27     }
28
29     public double getD() {
30         return d;
31     }
32
33     public int getX(final int y) {
34         if (height == 0)
35             return (int) (x2 + x1) / 2;
36
37         final int distanceFromY1 = y - (int) y1;
38
39         d = d1 + ((dinc * distanceFromY1) / height);
40
41         return (int) x1 + ((width * distanceFromY1) / height);
42     }
43
44     public void setPoints(final double x1, final double y1, final double d1,
45                           final double x2, final double y2, final double d2) {
46
47         this.x1 = x1;
48         this.y1 = y1;
49         this.d1 = d1;
50
51         this.x2 = x2;
52         this.y2 = y2;
53         this.d2 = d2;
54
55         height = (int) y2 - (int) y1;
56         width = (int) x2 - (int) x1;
57
58         dinc = d2 - d1;
59     }
60 }