Skip to content

Commit

Permalink
Plot point API for GraphRenderer, testing with plotting points on a l…
Browse files Browse the repository at this point in the history
…inear/logarithmic scale
  • Loading branch information
RyanMan56 committed Jan 10, 2021
1 parent fc3ac98 commit 6f88c37
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 46 deletions.
4 changes: 4 additions & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_15
targetCompatibility JavaVersion.VERSION_15
}
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package geometry;
package com.rbj_games.idle_siege.geometry;

public class Vector4 {
public float x, y, z, w;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
package com.rbj_games.idle_siege.utils;
package com.rbj_games.idle_siege.graphs;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Vector2;
Expand All @@ -21,7 +16,9 @@
import com.rbj_games.idle_siege.utils.Enums.Align;
import com.rbj_games.idle_siege.utils.Enums.Axis;

import geometry.Vector4;
import com.rbj_games.idle_siege.geometry.Vector4;
import com.rbj_games.idle_siege.utils.ScaleType;
import com.rbj_games.idle_siege.utils.Utils;

// If ScaleType is LINEAR_MINUTES then all values are in seconds, but displayed in minutes on the graph
public class GraphRenderer {
Expand All @@ -35,17 +32,6 @@ public class GraphRenderer {
private Color transparentBlack = new Color(0, 0, 0, 0.2f);
private List<Point> points;

public class Point {
public Vector2 value;
public Vector2 position;

public Point(Vector2 value, Vector2 position) {
this.value = value;
this.position = position;
}
}


// In the case of a log scale, the intervals are taken at every processed log.
// e.g. interval = 1. 10 ln = 1, 100 ln = 2. 10, 100 get displayed on the axis.
// 10^1 = 10, 10^2 = 100
Expand All @@ -64,7 +50,7 @@ public GraphRenderer(IdleSiege game, Map<IDrawable, IDrawable> textDrawables, Ve
dashes = new ArrayList<Vector4>();
points = new ArrayList<Point>();

setupPoints();
// setupPoints();

setupAxis(this.rangeX, this.intervals.x, this.scaleTypeX, Axis.X);
setupAxis(this.rangeY, this.intervals.y, this.scaleTypeY, Axis.Y);
Expand Down Expand Up @@ -130,24 +116,41 @@ public void setupDashedLines(Color color, Vector2 dashes, Vector2 start, Vector2
curLen += (dashes.x + dashes.y);
}
}

private void setupPoints() {
ArrayList<Vector2> values = new ArrayList<Vector2>();
values.add(new Vector2(0, 0));
values.add(new Vector2(1, 1));
values.add(new Vector2(2, 3));
values.add(new Vector2(2.5f, 2.5f));
values.add(new Vector2(3, 5));
values.add(new Vector2(4, 2));
values.add(new Vector2(2, 1.5f));
values.add(new Vector2(5, 1));

for (Vector2 val : values) {
float posX = Utils.ConvertRanges(rangeX.y, rangeX.x, position.x + size.x, position.x, val.x);
float posY = Utils.ConvertRanges(rangeY.y, rangeY.x, position.y + size.y, position.y, val.y);
points.add(new Point(val, new Vector2(posX, posY)));

private float plotPoint(ScaleType scaleType, float value, Vector2 range, float position, float size) {
switch (scaleType) {
case LOGARITHM:
return Utils.ConvertRanges(range.y, range.x, position + size, position, (float) Math.log10(value));
default:
return Utils.ConvertRanges(range.y, range.x, position + size, position, value);
}
}
}

public void addPoint(Vector2 value) {
float posX = plotPoint(scaleTypeX, value.x, rangeX, position.x, size.x);
float posY = plotPoint(scaleTypeY, value.y, rangeY, position.y, size.y);
points.add(new Point(value, new Vector2(posX, posY)));
}

// private void setupPoints() {
// ArrayList<Vector2> values = new ArrayList<Vector2>();
// values.add(new Vector2(0, 0));
// values.add(new Vector2(1, 10));
// values.add(new Vector2(2, 100));
// values.add(new Vector2(2.5f, 1000));
// values.add(new Vector2(3, 10000));
// values.add(new Vector2(4, 1000000));
// values.add(new Vector2(5, 10000000));
// System.out.println(scaleTypeX+" "+scaleTypeY);
//
// for (Vector2 val : values) {
//// float posX = Utils.ConvertRanges(rangeX.y, rangeX.x, position.x + size.x, position.x, val.x);
//// float posY = Utils.ConvertRanges(rangeY.y, rangeY.x, position.y + size.y, position.y, val.y);
// float posX = plotPoint(scaleTypeX, val.x, rangeX, position.x, size.x);
// float posY = plotPoint(scaleTypeY, val.y, rangeY, position.y, size.y);
// points.add(new Point(val, new Vector2(posX, posY)));
// }
// }

// private PolynomialSplineFunction interpolate()

Expand Down
13 changes: 13 additions & 0 deletions core/src/com/rbj_games/idle_siege/graphs/Point.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.rbj_games.idle_siege.graphs;

import com.badlogic.gdx.math.Vector2;

public class Point {
public Vector2 value;
public Vector2 position;

public Point(Vector2 value, Vector2 position) {
this.value = value;
this.position = position;
}
}
41 changes: 32 additions & 9 deletions core/src/com/rbj_games/idle_siege/screens/GameScreen.java
Original file line number Diff line number Diff line change
@@ -1,40 +1,63 @@
package com.rbj_games.idle_siege.screens;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.ScreenAdapter;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.math.Vector2;
import com.rbj_games.idle_siege.IDrawable;
import com.rbj_games.idle_siege.IdleSiege;
import com.rbj_games.idle_siege.utils.GraphRenderer;
import com.rbj_games.idle_siege.graphs.GraphRenderer;
import com.rbj_games.idle_siege.utils.ScaleType;

public class GameScreen extends ScreenAdapter {
IdleSiege game;
private Map<IDrawable, IDrawable> textDrawables;
private GraphRenderer graphRenderer;
long startTime = System.currentTimeMillis();
int lastX = 0;

public GameScreen(IdleSiege game) {
this.game = game;
textDrawables = new HashMap<IDrawable, IDrawable>();
Vector2 position = new Vector2(10f, 10f);
Vector2 size = new Vector2(80f, 60f);
Vector2 rangeX = new Vector2(0f, 5f);
Vector2 rangeY = new Vector2(0f, 5f);
Vector2 position = new Vector2(20f, 10f);
Vector2 size = new Vector2(60f, 60f);
Vector2 rangeX = new Vector2(0f, 20f);
Vector2 rangeY = new Vector2(0f, 10f);
Vector2 intervals = new Vector2(1f, 1f);
graphRenderer = new GraphRenderer(game, textDrawables, position, size, rangeX, rangeY, intervals, ScaleType.LINEAR, ScaleType.LINEAR);
graphRenderer = new GraphRenderer(game, textDrawables, position, size, rangeX, rangeY, intervals, ScaleType.LINEAR, ScaleType.LOGARITHM);
// graphRenderer.addPoint(new Vector2(0, 1));
// graphRenderer.addPoint(new Vector2(1, 3));
// graphRenderer.addPoint(new Vector2(2, 4));
}

private float graphFn(float x) {
float y = (float) Math.pow(x, 3);
// float y = (float) Math.sin(x);
// float y = (float) Math.pow(10, x);
// float y = (float) Math.tan(x);
// float y = x;
return y;
}

@Override
public void render(float delta) {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);


long totalTime = (System.currentTimeMillis() - startTime) / 1000;
System.out.println(totalTime);
if (totalTime > 1) {
float x = lastX++;
graphRenderer.addPoint(new Vector2(x, graphFn(x)));
startTime = System.currentTimeMillis();
}


graphRenderer.draw();

game.batch.setProjectionMatrix(game.camera.combined);
Expand Down

0 comments on commit 6f88c37

Please sign in to comment.