Updated readability of the code.
[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                 return y <= y2;
21         } else if (y >= y2)
22             return y <= y1;
23
24         return false;
25     }
26
27     public double getD() {
28         return d;
29     }
30
31     public int getX(final int y) {
32         if (height == 0)
33             return (int) (x2 + x1) / 2;
34
35         final int distanceFromY1 = y - (int) y1;
36
37         d = d1 + ((dinc * distanceFromY1) / height);
38
39         return (int) x1 + ((width * distanceFromY1) / height);
40     }
41
42     public void setPoints(final double x1, final double y1, final double d1,
43                           final double x2, final double y2, final double d2) {
44
45         this.x1 = x1;
46         this.y1 = y1;
47         this.d1 = d1;
48
49         this.x2 = x2;
50         this.y2 = y2;
51         this.d2 = d2;
52
53         height = (int) y2 - (int) y1;
54         width = (int) x2 - (int) x1;
55
56         dinc = d2 - d1;
57     }
58 }