Last active
November 17, 2017 20:58
-
-
Save bahmanm/c126be74389a95b5c0a2d3f7a996f8e4 to your computer and use it in GitHub Desktop.
Convert dates to alien looking plots using partial sums - https://github.com/bahmanm/datedreamer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// This has evolved into a fully-fledged project called DateDreamer. | |
// Please check the project's page for releases and more information. | |
// https://github.com/bahmanm/datedreamer | |
// | |
@Grab(group='com.github.yannrichet', module='JMathPlot', version='1.0.1') | |
@Grab(group='org.apache.commons', module='commons-math3', version='3.6.1') | |
import static java.lang.Math.* | |
import org.apache.commons.math3.complex.Complex | |
import javax.swing.JFrame | |
import java.awt.Color | |
import org.math.plot.* | |
/** | |
* Convert dates to alien looking diagram using partial sums. | |
* I got the idea from John D. Cook. | |
* | |
* @author Bahman Movaqar <Bahman@BahmanM.com> | |
*/ | |
class ShapeOfTheDate { | |
final static Complex CJ = new Complex(0.0, 1.0) | |
final static int N = 20_000 | |
final static int LEAP = 3 | |
final static String TITLE = 'Shape of the date' | |
int yy, mm, dd | |
double[] xs = new double[N] | |
double[] ys = new double[N] | |
double maxx, maxy, minx, miny | |
private ShapeOfTheDate(int y, int m, int d) { | |
yy = y ** 0.63 | |
mm = (d+m) ** 1.1 | |
dd = (d+y) ** 1.16 | |
} | |
Complex compOne(double n) { | |
double tmp = (n / mm) + ((n ** 2) / yy) + ((n ** 3) / dd) | |
CJ | |
.multiply(2 * PI * tmp) | |
.exp() | |
} | |
void compAll() { | |
Complex sum = Complex.ZERO | |
for (int i=LEAP; i<N+LEAP; i++) { | |
sum = sum.add(compOne(i)) | |
xs[i-LEAP] = sum.real; ys[i-LEAP] = sum.imaginary | |
maxx = max(sum.real, maxx); maxy = max(sum.imaginary, maxy) | |
minx = min(sum.real, minx); miny = min(sum.imaginary, miny) | |
} | |
} | |
void showPlot() { | |
double[] mins = [minx-3.0, miny-3.0] | |
double[] maxs = [maxx+3.0, maxy+3.0] | |
Plot2DPanel plot = new Plot2DPanel( | |
mins, maxs, | |
['LIN', 'LIN'] as String[], | |
['LIN', 'LIN'] as String[] | |
); | |
plot.addLinePlot(TITLE, new Color(108, 0, 143), xs, ys); | |
plot.setFixedBounds(mins, maxs); | |
plot.removeLegend() | |
new JFrame(TITLE).with { f -> | |
f.setSize(abs(plotDims()[0]), abs(plotDims()[1])) | |
f.contentPane = plot | |
f.visible = true | |
} | |
} | |
int[] plotDims() { | |
int width = (maxx - minx) * 10 | |
int height = ((maxy - miny) / (maxx - minx)) * width + 100 | |
[width, height] | |
} | |
void doGenerate() { | |
compAll() | |
showPlot() | |
} | |
static void generate() { | |
def date = new Date() | |
new ShapeOfTheDate(date.year-100, date.month+1, date.date).doGenerate() | |
} | |
} | |
ShapeOfTheDate.generate() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment