From c2b17e0f23b469ab1c108047acfe002531a57951 Mon Sep 17 00:00:00 2001 From: Vladimir Jovanovic Date: Thu, 26 Jan 2017 16:37:29 +0100 Subject: [PATCH] Abstract functions getGraphX() and getGraphY() now receive value of getT() More comments in code --- README.md | 2 +- lemniscate/build.gradle | 4 +- lemniscate/src/main/AndroidManifest.xml | 11 ++---- .../lemniscate/BernoullisProgressView.java | 7 +--- .../lemniscate/GeronosProgressView.java | 6 +-- .../base/BaseCurveProgressView.java | 38 +++++++++++++++---- .../funny/CannabisProgressView.java | 6 +-- .../lemniscate/funny/HeartProgressView.java | 6 +-- .../roulette/BaseRouletteProgressView.java | 2 +- .../roulette/EpitrochoidProgressView.java | 6 +-- .../roulette/HypotrochoidProgressView.java | 6 +-- 11 files changed, 50 insertions(+), 44 deletions(-) diff --git a/README.md b/README.md index 308b681..047cd92 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ Add to your module's build.gradle: and to your app build.gradle: dependencies { - compile 'com.github.vlad1m1r990:Lemniscate:1.0.2' + compile 'com.github.vlad1m1r990:Lemniscate:1.1.0' } Usage diff --git a/lemniscate/build.gradle b/lemniscate/build.gradle index dabbf9d..8584a12 100644 --- a/lemniscate/build.gradle +++ b/lemniscate/build.gradle @@ -7,8 +7,8 @@ android { defaultConfig { minSdkVersion 11 targetSdkVersion 25 - versionCode 100 - versionName "1.0.0" + versionCode 110 + versionName "1.1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" diff --git a/lemniscate/src/main/AndroidManifest.xml b/lemniscate/src/main/AndroidManifest.xml index 86a515b..e75170d 100644 --- a/lemniscate/src/main/AndroidManifest.xml +++ b/lemniscate/src/main/AndroidManifest.xml @@ -1,11 +1,8 @@ - - - + - diff --git a/lemniscate/src/main/java/com/vlad1m1r/lemniscate/BernoullisProgressView.java b/lemniscate/src/main/java/com/vlad1m1r/lemniscate/BernoullisProgressView.java index eece71c..45ec5b8 100644 --- a/lemniscate/src/main/java/com/vlad1m1r/lemniscate/BernoullisProgressView.java +++ b/lemniscate/src/main/java/com/vlad1m1r/lemniscate/BernoullisProgressView.java @@ -23,14 +23,11 @@ public BernoullisProgressView(Context context, AttributeSet attrs, int defStyleA super(context, attrs, defStyleAttr); } - public double getGraphY(int i){ - double t = getT(i); + public double getGraphY(double t){ return (mLemniscateParamY * Math.sin(t) * Math.cos(t)) / (1 + Math.pow(Math.sin(t), 2)); } - public double getGraphX(int i){ - // function is repeating every 2π and is defined from [0, 2π] so this is putting i∈[0, mPrecision) points between these two values - double t = getT(i); + public double getGraphX(double t){ // trigonometric function for value of x for t∈[0, 2π) return (mLemniscateParamX * Math.cos(t)) / (1 + Math.pow(Math.sin(t), 2)); } diff --git a/lemniscate/src/main/java/com/vlad1m1r/lemniscate/GeronosProgressView.java b/lemniscate/src/main/java/com/vlad1m1r/lemniscate/GeronosProgressView.java index 3634cf3..6b4d8e6 100644 --- a/lemniscate/src/main/java/com/vlad1m1r/lemniscate/GeronosProgressView.java +++ b/lemniscate/src/main/java/com/vlad1m1r/lemniscate/GeronosProgressView.java @@ -24,14 +24,12 @@ public GeronosProgressView(Context context, AttributeSet attrs, int defStyleAttr } @Override - public double getGraphY(int i) { - double t = getT(i); + public double getGraphY(double t) { return mLemniscateParamX * Math.sin(t) * Math.cos(t); } @Override - public double getGraphX(int i) { - double t = getT(i); + public double getGraphX(double t) { return mLemniscateParamX * Math.sin(t); } } diff --git a/lemniscate/src/main/java/com/vlad1m1r/lemniscate/base/BaseCurveProgressView.java b/lemniscate/src/main/java/com/vlad1m1r/lemniscate/base/BaseCurveProgressView.java index fbf4bfd..b5a9c8b 100644 --- a/lemniscate/src/main/java/com/vlad1m1r/lemniscate/base/BaseCurveProgressView.java +++ b/lemniscate/src/main/java/com/vlad1m1r/lemniscate/base/BaseCurveProgressView.java @@ -41,6 +41,9 @@ public abstract class BaseCurveProgressView extends View { protected float mStrokeWidth = getResources().getDimension(R.dimen.lemniscate_stroke_width); + /** + * Default size of view will be multiplied with this number + */ protected float mSizeMultiplier = 1; protected double mLemniscateParamX, mLemniscateParamY; @@ -56,6 +59,9 @@ public abstract class BaseCurveProgressView extends View { */ protected float mLineMinLength = 0.4f, mLineMaxLength = 0.8f; + /** + * If true, the line length will oscillate between mLineMinLength and mLineMaxLength + */ protected boolean mIsLineLengthChangeable = true; /** @@ -149,9 +155,21 @@ private void init() { mPaint.setStrokeCap(Paint.Cap.ROUND); } - public abstract double getGraphY(int i); + /** + * This method should return values of y for t∈[0, upper limit of getT() function]. + * We should use parametric representation of curve for y. + * Curve should be closed and periodic on interval that returns getT(). + * Resulting value should satisfy y∈[-mLemniscateParamY, mLemniscateParamY]. + */ + public abstract double getGraphY(double t); - public abstract double getGraphX(int i); + /** + * This method should return values of x for t∈[0, upper limit of getT() function]. + * We should use parametric representation of curve for x. + * Curve should be closed and periodic on interval that returns getT(). + * Resulting value should satisfy x∈[-mLemniscateParamX, upper limit of getT() function]. + */ + public abstract double getGraphX(double t); @Override protected void onDraw(Canvas canvas) { @@ -216,8 +234,8 @@ private int getPointsOnCurve(ArrayList> list, @Nullable Integ for (int i = start != null ? start : 0; i < mPrecision; i++) { // translates points to positive coordinates - double x = getGraphX(i) + mLemniscateParamX; - double y = getGraphY(i) + mLemniscateParamY; + double x = getGraphX(getT(i)) + mLemniscateParamX; + double y = getGraphY(getT(i)) + mLemniscateParamY; addPointToList(list, x, y); @@ -232,15 +250,14 @@ private int getPointsOnCurve(ArrayList> list, @Nullable Integ private void addPointToList(ArrayList> list, double x, double y) { - //finds smallest ratio for which lemniscate should be resized because of stroke width - //it's not just 1 * mStrokeWidth because it's behavior is strange for tick lines + //finds smallest ratio for which curve should be resized because of stroke width float ratio = mViewHeight/(mViewHeight + 2 * mStrokeWidth); //move every point for ratio x = x * ratio; y = y * ratio; - //moves points so that lemniscate is centered + //moves points so that curve is centered x = x + mStrokeWidth * ratio; y = y + mStrokeWidth * ratio; @@ -544,7 +561,12 @@ public void setPrecision(int precision) { invalidate(); } - public double getT(int i) { + + /** + * @param i ∈ [0, mPrecision) + * @return function is putting i∈[0, mPrecision) points between [0, 2π] + */ + protected double getT(int i) { return i*2*Math.PI/mPrecision; } } \ No newline at end of file diff --git a/lemniscate/src/main/java/com/vlad1m1r/lemniscate/funny/CannabisProgressView.java b/lemniscate/src/main/java/com/vlad1m1r/lemniscate/funny/CannabisProgressView.java index a09c9d8..120c932 100644 --- a/lemniscate/src/main/java/com/vlad1m1r/lemniscate/funny/CannabisProgressView.java +++ b/lemniscate/src/main/java/com/vlad1m1r/lemniscate/funny/CannabisProgressView.java @@ -24,14 +24,12 @@ public CannabisProgressView(Context context, AttributeSet attrs, int defStyleAtt } @Override - public double getGraphY(int i) { - double t = getT(i); + public double getGraphY(double t) { return -mLemniscateParamY/3 * Math.sin(t) * (Math.sin(t) + 1) * (9/10f * Math.cos(8*t) + 1) * (1/10f * Math.cos(24*t) + 1) * (1/10f * Math.cos(200*t) + 9/10f) + mLemniscateParamY/2; } @Override - public double getGraphX(int i) { - double t = getT(i); + public double getGraphX(double t) { return mLemniscateParamX/3 * (Math.sin(t) + 1) * Math.cos(t) * (9/10f * Math.cos(8*t) + 1) * (1/10f * Math.cos(24*t) + 1) * (1/10f * Math.cos(200*t) + 9/10f); } } diff --git a/lemniscate/src/main/java/com/vlad1m1r/lemniscate/funny/HeartProgressView.java b/lemniscate/src/main/java/com/vlad1m1r/lemniscate/funny/HeartProgressView.java index 40d4aa7..9669ba4 100644 --- a/lemniscate/src/main/java/com/vlad1m1r/lemniscate/funny/HeartProgressView.java +++ b/lemniscate/src/main/java/com/vlad1m1r/lemniscate/funny/HeartProgressView.java @@ -24,14 +24,12 @@ public HeartProgressView(Context context, AttributeSet attrs, int defStyleAttr) } @Override - public double getGraphY(int i) { - double t = getT(i); + public double getGraphY(double t) { return -mLemniscateParamY/17 * (13 * Math.cos(t) - 5 * Math.cos(2*t) - 2 * Math.cos(3*t) - Math.cos(4*t)); } @Override - public double getGraphX(int i) { - double t = getT(i); + public double getGraphX(double t) { return mLemniscateParamX/17 * 16 * Math.pow(Math.sin(t), 3); } } diff --git a/lemniscate/src/main/java/com/vlad1m1r/lemniscate/roulette/BaseRouletteProgressView.java b/lemniscate/src/main/java/com/vlad1m1r/lemniscate/roulette/BaseRouletteProgressView.java index 0266d1f..b458eab 100644 --- a/lemniscate/src/main/java/com/vlad1m1r/lemniscate/roulette/BaseRouletteProgressView.java +++ b/lemniscate/src/main/java/com/vlad1m1r/lemniscate/roulette/BaseRouletteProgressView.java @@ -94,7 +94,7 @@ public void setNumberOfCycles(float numberOfCycles) { @Override public double getT(int i) { - return i* mNumberOfCycles *2*Math.PI/mPrecision; + return i * mNumberOfCycles * 2 * Math.PI / mPrecision; } @Override diff --git a/lemniscate/src/main/java/com/vlad1m1r/lemniscate/roulette/EpitrochoidProgressView.java b/lemniscate/src/main/java/com/vlad1m1r/lemniscate/roulette/EpitrochoidProgressView.java index cdf1a41..953df4e 100644 --- a/lemniscate/src/main/java/com/vlad1m1r/lemniscate/roulette/EpitrochoidProgressView.java +++ b/lemniscate/src/main/java/com/vlad1m1r/lemniscate/roulette/EpitrochoidProgressView.java @@ -24,16 +24,14 @@ public EpitrochoidProgressView(Context context, AttributeSet attrs, int defStyle } @Override - public double getGraphY(int i) { + public double getGraphY(double t) { //y = (mRadiusFixed + mRadiusMoving) sin(t) - mDistanceFromCenter sin(((mRadiusFixed+mRadiusMoving)/mRadiusMoving)*t) - double t = getT(i); return mLemniscateParamY/((mRadiusFixed + mDistanceFromCenter + mRadiusMoving))*((mRadiusFixed + mRadiusMoving)*Math.sin(t) - mDistanceFromCenter *Math.sin(((mRadiusFixed + mRadiusMoving)/ mRadiusMoving)*t)); } @Override - public double getGraphX(int i) { + public double getGraphX(double t) { //x = (mRadiusFixed + mRadiusMoving) cos(t) + mRadiusMoving cos(((mRadiusFixed+mRadiusMoving)/mRadiusMoving)*t), - double t = getT(i); return mLemniscateParamY/((mRadiusFixed + mDistanceFromCenter + mRadiusMoving))*((mRadiusFixed + mRadiusMoving)*Math.cos(t) - mDistanceFromCenter *Math.cos(((mRadiusFixed + mRadiusMoving)/ mRadiusMoving)*t)); } diff --git a/lemniscate/src/main/java/com/vlad1m1r/lemniscate/roulette/HypotrochoidProgressView.java b/lemniscate/src/main/java/com/vlad1m1r/lemniscate/roulette/HypotrochoidProgressView.java index 47315f5..1bce1e5 100644 --- a/lemniscate/src/main/java/com/vlad1m1r/lemniscate/roulette/HypotrochoidProgressView.java +++ b/lemniscate/src/main/java/com/vlad1m1r/lemniscate/roulette/HypotrochoidProgressView.java @@ -24,16 +24,14 @@ public HypotrochoidProgressView(Context context, AttributeSet attrs, int defStyl } @Override - public double getGraphY(int i) { + public double getGraphY(double t) { //y = (mRadiusFixed - mRadiusMoving) sin(t) - mRadiusMoving sin(((mRadiusFixed-mRadiusMoving)/mRadiusMoving)*t) - double t = getT(i); return mLemniscateParamY/((mRadiusFixed + mDistanceFromCenter - mRadiusMoving))*((mRadiusFixed - mRadiusMoving)*Math.sin(t) + mDistanceFromCenter *Math.sin(((mRadiusFixed - mRadiusMoving)/ mRadiusMoving)*t)); } @Override - public double getGraphX(int i) { + public double getGraphX(double t) { //x = (mRadiusFixed - mRadiusMoving) cos(t) + mRadiusMoving cos(((mRadiusFixed-mRadiusMoving)/mRadiusMoving)*t), - double t = getT(i); return mLemniscateParamY/((mRadiusFixed + mDistanceFromCenter - mRadiusMoving))*((mRadiusFixed - mRadiusMoving)*Math.cos(t) - mDistanceFromCenter *Math.cos(((mRadiusFixed - mRadiusMoving)/ mRadiusMoving)*t)); }