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