Skip to content

Commit

Permalink
Merge pull request #7 from gillie04th/correction-calcul-externes
Browse files Browse the repository at this point in the history
corrections calculs externes db
  • Loading branch information
gillie04th authored May 17, 2024
2 parents 4803d95 + 44b1b5e commit 3f5876c
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public ArrayList<Point> getEpicycloidCoordinatesById(@PathVariable int id, @Path
@ResponseBody
public ArrayList<Point> getEpicycloidCoordinates(@RequestBody Epicycloid epicycloid, @PathVariable int pointsNumber) {

System.out.println(epicycloid);
return epicycloid.getCoordinates(pointsNumber);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
import jakarta.validation.Valid;
import org.epicycloide_back.epicycloide_back.validation.GreaterThan;
import org.springframework.validation.annotation.Validated;
import org.epicycloide_back.epicycloide_back.util.FractionConverter;

import java.util.ArrayList;
import java.util.HashMap;

@Entity
public class Epicycloid {
Expand All @@ -15,9 +18,15 @@ public class Epicycloid {
@Column(name = "id")
private int id;

@GreaterThan(limit=0.0)
private String name;

@GreaterThan(limit = 0.0)
private Double radius;

@Column(nullable = true)
@GreaterThan(limit = 0.0)
private Double frequency;

@OneToOne(cascade = {CascadeType.ALL})
@JoinColumn(name = "rolling_id")
@Valid
Expand All @@ -28,12 +37,6 @@ public class Epicycloid {
@Valid
private Epicycloid fixed;

private String name;

@Column(nullable = true)
@GreaterThan(limit = 0.0)
private Double frequency;


public void setId(int id) {
this.id = id;
Expand Down Expand Up @@ -89,32 +92,39 @@ public ArrayList<Point> getCoordinates(int pointsNumber) {

for (double i = 0; i < pointsNumber; i++) {

double t = (double) 2 * Math.PI * i / pointsNumber;

Epicycloid rolling = this;
Epicycloid fixed = null;
double baseFrequency = rolling.getFrequency();

double x = 0;
double y = 0;
// double t = 0;
double frequencySum = 0;

double t = (double) 2 * Math.PI * i / pointsNumber;

while (rolling != null) {


frequencySum += rolling.getFrequency();

if(rolling.getFixed() == null) {
if (fixed == null) {

// t = (double) 2 * Math.PI * i / pointsNumber;
x += rolling.getRadius() * Math.cos(t);
y += rolling.getRadius() * Math.sin(t);

} else {

// FractionConverter.decimalToFraction(rolling.getRadius() / rolling.getFixed().getRadius())[1] *
// t = (double) 2 * Math.PI * i / pointsNumber;
x += rolling.getRadius() * Math.cos(frequencySum / baseFrequency * t);
y += rolling.getRadius() * Math.sin(frequencySum / baseFrequency * t);

}

rolling = rolling.getRolling();
fixed = rolling;
}

Point point = new Point(x, y, t);
Expand All @@ -126,4 +136,8 @@ public ArrayList<Point> getCoordinates(int pointsNumber) {

}

@Override
public String toString() {
return "Epicycloid [id=" + id + ", name=" + name + ", radius=" + radius + ", frequency=" + frequency + ", rolling=" + rolling;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.epicycloide_back.epicycloide_back.util;

public class FractionConverter {

static long gcd(long a, long b) {
if (a == 0)
return b;
else if (b == 0)
return a;
if (a < b)
return gcd(a, b % a);
else
return gcd(b, a % b);
}

public static Long[] decimalToFraction(double number) {

// Fetch integral value of the decimal
double intVal = Math.floor(number);

// Fetch fractional part of the decimal
double fVal = number - intVal;

// Consider precision value to convert fractional part to integral equivalent
final long pVal = 1000000000;

// Calculate GCD of integral equivalent of fractional part and precision value
long gcdVal = gcd(Math.round(
fVal * pVal), pVal);

// Calculate num and deno
long num = Math.round(fVal * pVal) / gcdVal;
long deno = pVal / gcdVal;

return new Long[]{num, deno};
}
}

0 comments on commit 3f5876c

Please sign in to comment.