Skip to content

Instantly share code, notes, and snippets.

@paramsen
Last active August 3, 2017 08:01
Show Gist options
  • Save paramsen/64b4a2ba935b0df6c3cbaa8f1ca8b2a9 to your computer and use it in GitHub Desktop.
Save paramsen/64b4a2ba935b0df6c3cbaa8f1ca8b2a9 to your computer and use it in GitHub Desktop.
Android: Simple trigonometric formula for calculating cascading joint rotations for an arm joint system.
// This is free and unencumbered software released into the public domain.
//
// Anyone is free to copy, modify, publish, use, compile, sell, or
// distribute this software, either in source code form or as a compiled
// binary, for any purpose, commercial or non-commercial, and by any
// means.
//
// In jurisdictions that recognize copyright laws, the author or authors
// of this software dedicate any and all copyright interest in the
// software to the public domain. We make this dedication for the benefit
// of the public at large and to the detriment of our heirs and
// successors. We intend this dedication to be an overt act of
// relinquishment in perpetuity of all present and future rights to this
// software under copyright law.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
// For more information, please refer to <https://unlicense.org>
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
/**
* Hard coded the distances in pixels and stuff, it's just realized pseudocode so expect to sanitize the code
* for the Android realm. "Buttons" are also hard coded into the view for simplicity.
*
* @author Pär Amsen 07/2017
*/
public class CraneView extends View {
float[] parent; //0: posX, 1: posY, 2: rot
float[] child1; //0: offset, 1: rot
float[] child2; //0: offset
RectF button1;
RectF button2;
Paint linePaint;
Paint textPaint;
public CraneView(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
parent = new float[]{200, 500, -200};
child1 = new float[]{100, 45};
child2 = new float[]{75};
button1 = new RectF(300, 200, 300 + 300, 200 + 50);
button2 = new RectF(300, 300, 300 + 300, 300 + 50);
linePaint = new Paint();
linePaint.setColor(Color.BLACK);
linePaint.setStyle(Paint.Style.STROKE);
linePaint.setStrokeWidth(10);
linePaint.setTextSize(20);
textPaint = new Paint();
textPaint.setColor(Color.BLACK);
textPaint.setTextSize(20);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
float c1X = parent[0] + child1[0] * ((float) Math.sin(Math.toRadians(parent[2])));
float c1Y = parent[1] + child1[0] * ((float) Math.cos(Math.toRadians(parent[2])));
float c2X = c1X + child2[0] * (((float) Math.sin(Math.toRadians(child1[1] + parent[2]))));
float c2Y = c1Y + child2[0] * (((float) Math.cos(Math.toRadians(child1[1] + parent[2]))));
canvas.drawLine(parent[0],
parent[1],
c1X,
c1Y,
linePaint);
canvas.drawLine(c1X,
c1Y,
c2X,
c2Y,
linePaint);
canvas.drawRect(button1, linePaint);
canvas.drawText("Rotate parent joint", button1.left, button1.bottom - 25, textPaint);
canvas.drawRect(button2, linePaint);
canvas.drawText("Rotate first joint", button2.left, button2.bottom - 25, textPaint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
if (button1.contains(x, y))
parent[2] += 10;
if (button2.contains(x, y))
child1[1] += 10;
postInvalidate();
return super.onTouchEvent(event);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment