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