Coder Social home page Coder Social logo

java-android-bezier-curve-implementation-using-interpolation's Introduction

Java-Android-Bezier-Curve-Implementation-Using-Interpolation

Bezier curve implementation from scratch in Java android using interpolation.

Description

Linear interpolation also known as lerp function in programming languages::

public Point linearInterpolation(float t, Point start, Point stop) {
  float Px = (1 - t) * start.x + t * stop.x;
  float Py = (1 - t) * start.y + t * stop.y;
  Point P = new Point((int) Px, (int) Py);
  return P;
}

There are three parameters, the first one is t value from 0 to 1 , which calculates the point between the start and stop points of the line .
For example, let's set two points on the coordinate (x, y):
A=(100,100) and
B=(200,200)
Now we have two points, and let's say that:
Start point is A
End point is B
Now let's set the value to t = 0.5 and add these t, A and B three parameters to our linearInterpolation function

linearInterpolation(t, A, B)

then the linearInterpolation function will return us the point between A and B:

(150,150)

Examples

According to the rule t, it should be between 0 ≤ t ≤ 1, but not 0 > t > 1.

Example №1:

float t = 0.664f;
Point A = new Point(45, 100);
Point B = new Point(784, 135);
Point C = linearInterpolation(t, A, B);
System.out.println("C(x=" + C.x + ", y=" + C.y + ")");
// Output: C(x=535, y=123)

Example №2:

float t = 0.91564644f;
Point A = new Point(876879, 54678);
Point B = new Point(786874, 786135);
Point C = linearInterpolation(t, A, B);
System.out.println("C(x=" + C.x + ", y=" + C.y + ")");
// Output:  C(x=794466, y=724434)

Example №3:

float t = 0.5f;
Point A = new Point(0, 100);
Point B = new Point(100, 0);
Point C = linearInterpolation(t, A, B);
System.out.println("C(x=" + C.x + ", y=" + C.y + ")");
// Output:  C(x=50, y=50)

if t<0:

float t = -0.5f;
Point A = new Point(0, 100);
Point B = new Point(100, 0);
Point C = linearInterpolation(t, A, B);
System.out.println("C(x=" + C.x + ", y=" + C.y + ")");
// Output: C(x=-50,y=150)

if t=0:

float t = 0.0f;
Point A = new Point(0, 100);
Point B = new Point(100, 0);
Point C = linearInterpolation(t, A, B);
System.out.println("C(x=" + C.x + ", y=" + C.y + ")");
// Output: C(x=0,y=100)

if t=1:

float t = 1.0f;
Point A = new Point(0, 100);
Point B = new Point(100, 0);
Point C = linearInterpolation(t, A, B);
System.out.println("C(x=" + C.x + ", y=" + C.y + ")");
// Output: C(x=100,y=0)

if t>1:

float t = 1.5f;
Point A = new Point(0, 100);
Point B = new Point(100, 0);
Point C = linearInterpolation(t, A, B);
System.out.println("C(x=" + C.x + ", y=" + C.y + ")");
// Output: C(x=150,y=-50)

Demonstration:

https://youtube.com/shorts/xUuIoobyBQQ?feature=share Watch the video

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.