Ajout du Realtime ainsi que de nouvelles fonctions de distance et de génération d'images

This commit is contained in:
Mysaa 2021-05-27 21:25:48 +02:00
parent 0d76df4ea4
commit e4733145cc
8 changed files with 1330 additions and 25 deletions

View File

@ -30,7 +30,7 @@ public class TestDistance {
}
}
if (F[n].coords.length > Dmax) {
System.out.println("Dmax passe de " + Dmax + " à " + F[n].coords.length);
System.out.println("Dmax passe de " + Dmax + " <EFBFBD> " + F[n].coords.length);
Dmax = F[n].coords.length;
}
}
@ -44,6 +44,35 @@ public class TestDistance {
return sigmaN;
}
public static double getDistance(Point A, List<Point> F, double P) {
double sigmaN = 0.0d, sigmaD = 0.0d;
int N = F.size(), D = A.getCoords().length, Dmax = D;
for (int n = 0; n < N; n++) {
if (F.get(n).coords.length != D) {
int[] change = new int[Dmax];
if (F.get(n).coords.length != Dmax) {
for (int dm = 0; dm < Dmax; dm++)
change[dm] = (dm < F.get(n).coords.length) ? (F.get(n).getCoord(dm + 1)) : 0;
F.get(n).coords = change;
}
}
if (F.get(n).coords.length > Dmax) {
System.out.println("Dmax passe de " + Dmax + " <20> " + F.get(n).coords.length);
Dmax = F.get(n).coords.length;
}
}
for (int n = 0; n < N; n++) {
sigmaD = 0.0d;
for (int d = 1; d <= D; d++)
sigmaD += pow(abs(A.getCoord(d) - F.get(n).getCoord(d)), P);
sigmaN += pow(sigmaD, 1.0 / P);
}
return sigmaN;
}
// lorsque P tend vers PosInf
public static double getDistancePI(Point A, Point[] F) {
@ -60,7 +89,7 @@ public class TestDistance {
}
}
if (F[n].coords.length > Dmax) {
System.out.println("Dmax passe de " + Dmax + " à " + F[n].coords.length);
System.out.println("Dmax passe de " + Dmax + " <EFBFBD> " + F[n].coords.length);
Dmax = F[n].coords.length;
}
}
@ -91,7 +120,7 @@ public class TestDistance {
}
}
if (F[n].coords.length > Dmax) {
System.out.println("Dmax passe de " + Dmax + " à " + F[n].coords.length);
System.out.println("Dmax passe de " + Dmax + " <EFBFBD> " + F[n].coords.length);
Dmax = F[n].coords.length;
}
}

View File

@ -1,12 +1,14 @@
package com.bernard.cercloide.factory;
import static java.lang.Math.*;
import static java.lang.Math.abs;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.JOptionPane;
@ -196,7 +198,7 @@ public class ImageCreator
}
System.out.println("Image crée");
System.out.println("Image cr<EFBFBD>e");
try
{
@ -206,7 +208,7 @@ public class ImageCreator
}
catch(IOException e)
{
System.err.println("Erreur lers de la création de l'image au path " + imagePath.toString());
System.err.println("Erreur lers de la cr<EFBFBD>ation de l'image au path " + imagePath.toString());
e.printStackTrace();
}
System.out.println("Done");
@ -409,7 +411,7 @@ public class ImageCreator
}
}
System.out.println("Image crée");
System.out.println("Image cr<EFBFBD>e");
try
{
@ -417,7 +419,7 @@ public class ImageCreator
}
catch(IOException e)
{
System.err.println("Erreur lers de la création de l'image au path " + imagePath.toString());
System.err.println("Erreur lers de la cr<EFBFBD>ation de l'image au path " + imagePath.toString());
e.printStackTrace();
}
System.out.println("Done");
@ -432,6 +434,284 @@ public class ImageCreator
}
public static BufferedImage createImage(Point[] foyers, Double p, Color cercloidColor, long cercloidRayon, int cercloidFourch, int width, int height, Color foyColor, char foyCrossConvention, int foyCrossSize)
{
long rayonMax = cercloidRayon + cercloidFourch, rayonMin = cercloidRayon - cercloidFourch;
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
if(p.equals(Double.POSITIVE_INFINITY))
{
for(int w = 0; w < width; w++)
{
for(int h = 0; h < height; h++)
{
long dis = (long)TestDistance.getDistancePI(new Point(w, h), foyers);
if(rayonMin <= dis && dis <= rayonMax)
{
bi.setRGB(w, h, cercloidColor.getRGB());
}
else
{
bi.setRGB(w, h, Color.WHITE.getRGB());
}
bi.flush();
}
}
}
else if(p.equals(Double.NEGATIVE_INFINITY))
{
for(int w = 0; w < width; w++)
{
for(int h = 0; h < height; h++)
{
long dis = (long)TestDistance.getDistanceMI(new Point(w, h), foyers);
if(rayonMin <= dis && dis <= rayonMax)
{
bi.setRGB(w, h, cercloidColor.getRGB());
}
else
{
bi.setRGB(w, h, Color.WHITE.getRGB());
}
bi.flush();
}
}
}
else
{
for(int w = 0; w < width; w++)
{
for(int h = 0; h < height; h++)
{
long dis = (long)TestDistance.getDistance(new Point(w, h), foyers, p);
if(rayonMin <= dis && dis <= rayonMax)
{
bi.setRGB(w, h, cercloidColor.getRGB());
}
else
{
bi.setRGB(w, h, Color.WHITE.getRGB());
}
bi.flush();
}
}
}
/**
* String convention : "a"+45 == absolu de 45 "r"+142 == relatif de 12
* "d"+0 = valeur par defaut "n"+0 = pas de croix
*/
int pxMax = 0;
boolean doCross = true;
switch(foyCrossConvention)
{
case 'd':
pxMax = (int)((2.0 * (double)height) / 500.0);
break;
case 'a':
pxMax = foyCrossSize;
break;
case 'r':
pxMax = (int)(((double)foyCrossSize * (double)height) / 500.0);
break;
case 'n':
doCross = false;
break;
default:
break;
}
if(doCross)
{
for(Point f : foyers)
{
int x = f.getCoord(1);
int y = f.getCoord(2);
int rgb = foyColor.getRGB();
for(int i = 1; i <= pxMax; i++)
{
try
{
bi.setRGB(x - i, y, rgb);
bi.flush();
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Drawing cross out of bounds");
}
try
{
bi.setRGB(x, y - i, rgb);
bi.flush();
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Drawing cross out of bounds");
}
try
{
bi.setRGB(x + i, y, rgb);
bi.flush();
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Drawing cross out of bounds");
}
try
{
bi.setRGB(x, y + i, rgb);
bi.flush();
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Drawing cross out of bounds");
}
bi.setRGB(x, y, rgb);
bi.flush();
}
}
}
System.out.println("Done");
return bi;
}
public static void createImage(Graphics g,Point[] foyers, Double p, Color cercloidColor, long cercloidRayon, double cercloidFourch, int width, int height, Color foyColor, char foyCrossConvention, int foyCrossSize)
{
double rayonMax = cercloidRayon + cercloidFourch, rayonMin = cercloidRayon - cercloidFourch;
g.setColor(cercloidColor);
if(p.equals(Double.POSITIVE_INFINITY))
{
for(int w = 0; w < width; w++)
{
for(int h = 0; h < height; h++)
{
long dis = (long)TestDistance.getDistancePI(new Point(w, h), foyers);
if(rayonMin <= dis && dis <= rayonMax)
{
drawPixel(g,w, h);
}
}
}
}
else if(p.equals(Double.NEGATIVE_INFINITY))
{
for(int w = 0; w < width; w++)
{
for(int h = 0; h < height; h++)
{
long dis = (long)TestDistance.getDistanceMI(new Point(w, h), foyers);
if(rayonMin <= dis && dis <= rayonMax)
{
drawPixel(g,w, h);
}
}
}
}
else
{
for(int w = 0; w < width; w++)
{
for(int h = 0; h < height; h++)
{
long dis = (long)TestDistance.getDistance(new Point(w, h), foyers, p);
if(rayonMin <= dis && dis <= rayonMax)
{
drawPixel(g,w, h);
}
}
}
}
/**
* String convention : "a"+45 == absolu de 45 "r"+142 == relatif de 12
* "d"+0 = valeur par defaut "n"+0 = pas de croix
*/
int pxMax = 0;
boolean doCross = true;
switch(foyCrossConvention)
{
case 'd':
pxMax = (int)((2.0 * (double)height) / 500.0);
break;
case 'a':
pxMax = foyCrossSize;
break;
case 'r':
pxMax = (int)(((double)foyCrossSize * (double)height) / 500.0);
break;
case 'n':
doCross = false;
break;
default:
break;
}
if(doCross)
{
g.setColor(foyColor);
for(Point f : foyers)
{
int x = f.getCoord(1);
int y = f.getCoord(2);
for(int i = 1; i <= pxMax; i++)
{
try
{
drawPixel(g,x - i, y);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Drawing cross out of bounds");
}
try
{
drawPixel(g,x, y - i);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Drawing cross out of bounds");
}
try
{
drawPixel(g,x + i, y);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Drawing cross out of bounds");
}
try
{
drawPixel(g,x, y + i);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Drawing cross out of bounds");
}
drawPixel(g,x, y);
}
}
}
System.out.println("Done");
}
/**
* Create and save a Image af the cercloid specified with arguments (smart
* algo normalerweise)
@ -651,14 +931,14 @@ public class ImageCreator
}
}
System.out.println("Image crée");
System.out.println("Image cr<EFBFBD>e");
try
{
ImageIO.write(bi, "png", imagePath.toFile());
}
catch(IOException e)
{
System.err.println("Erreur lers de la création de l'image au path " + imagePath.toString());
System.err.println("Erreur lers de la cr<EFBFBD>ation de l'image au path " + imagePath.toString());
e.printStackTrace();
}
System.out.println("Done");
@ -668,23 +948,467 @@ public class ImageCreator
}
}
public static void createImage2(Graphics g, Point[] foyers, Double p, Color cercloidColor, long cercloidRayon, int width, int height, Color foyColor, char foyCrossConvention, int foyCrossSize)
{
System.out.println(Arrays.toString(foyers));
if(foyers.length==0)return;
g.setColor(cercloidColor);
if(p > 0.0)
{
Point barycentre = Point.findBarycentre(foyers);
g.setColor(Color.LIGHT_GRAY);
g.drawLine(0, barycentre.getCoord(2),1000,barycentre.getCoord(2));
g.drawLine(barycentre.getCoord(1), 0,barycentre.getCoord(1),1000);
g.setColor(cercloidColor);
Point pointNE = barycentre.clone();
Point pointNO = barycentre.clone();
Point pointSO = barycentre.clone();
Point pointSE = barycentre.clone();
double error = -1.0;
boolean flipped = false;
while(error < 0 && !flipped)
{
if(error>0)flipped=true;
pointNE.incrementCoord(1);
error = TestDistance.getDistance(pointNE, foyers, p) - cercloidRayon;
}
error = -1.0;
flipped=false;
while(error < 0)
{
if(error>0)flipped=true;
pointNO.incrementCoord(2);
error = TestDistance.getDistance(pointNO, foyers, p) - cercloidRayon;
}
error = -1.0;
flipped=false;
while(error < 0)
{
if(error>0)flipped=true;
pointSO.decrementCoord(1);
error = TestDistance.getDistance(pointSO, foyers, p) - cercloidRayon;
}
error = -1.0;
flipped=false;
while(error < 0)
{
if(error>0)flipped=true;
pointSE.decrementCoord(2);
error = TestDistance.getDistance(pointSE, foyers, p) - cercloidRayon;
}
pointNE.decrementCoord(1);
pointNO.decrementCoord(2);
pointSO.incrementCoord(1);
pointSE.incrementCoord(2);
Point firstPointNE = pointNE.clone();
System.out.println(pointNE + "," + pointNO + "," + pointSO + "," + pointSE);
// List<Point> figure = new ArrayList<>();
int[][] MODIFIED_COORDS = {{-1, 0}, {0, +1}, {-1, +1},{+1, +1},{-1, -1}};
g.setColor(Color.red);
while(pointNE.getCoord(1)>pointNO.getCoord(1))
{
// figure.add(pointNE);
System.out.println(pointNE);
try
{
drawPixel(g,pointNE.getCoord(1), pointNE.getCoord(2));
}
catch(ArrayIndexOutOfBoundsException e)
{
System.err.println("error");
}
pointNE = getBetter(foyers, p, cercloidRayon, pointNE.withModifiedCoords(MODIFIED_COORDS[0]), pointNE.withModifiedCoords(MODIFIED_COORDS[1]), pointNE.withModifiedCoords(MODIFIED_COORDS[2]));
}
MODIFIED_COORDS = new int[][] {{-1, 0}, {0, -1}, {-1, -1},{-1, +1},{+1, -1}};
g.setColor(Color.cyan);
while(pointNO.getCoord(2)>pointSO.getCoord(2))
{
// figure.add(pointNO);
try
{
drawPixel(g,pointNO.getCoord(1), pointNO.getCoord(2));
}
catch(ArrayIndexOutOfBoundsException e)
{
System.err.println("error");
}
pointNO = getBetter(foyers, p, cercloidRayon, pointNO.withModifiedCoords(MODIFIED_COORDS[0]), pointNO.withModifiedCoords(MODIFIED_COORDS[1]), pointNO.withModifiedCoords(MODIFIED_COORDS[2]));
}
g.setColor(Color.blue);
MODIFIED_COORDS = new int[][] {{+1, 0}, {0, -1}, {+1, -1},{-1, -1},{+1, +1}};
while(pointSO.getCoord(1)<pointSE.getCoord(1))
{
// figure.add(pointSO);
try
{
drawPixel(g,pointSO.getCoord(1), pointSO.getCoord(2));
}
catch(ArrayIndexOutOfBoundsException e)
{
System.err.println("error");
}
pointSO = getBetter(foyers, p, cercloidRayon, pointSO.withModifiedCoords(MODIFIED_COORDS[0]), pointSO.withModifiedCoords(MODIFIED_COORDS[1]), pointSO.withModifiedCoords(MODIFIED_COORDS[2]));
}
g.setColor(Color.orange);
MODIFIED_COORDS = new int[][] {{+1, 0}, {0, +1}, {+1, +1},{+1, -1},{-1, +1}};
while(pointSE.getCoord(2)<firstPointNE.getCoord(2))
{
// figure.add(pointSE);
try
{
drawPixel(g,pointSE.getCoord(1), pointSE.getCoord(2));
}
catch(ArrayIndexOutOfBoundsException e)
{
System.err.println("error");
}
pointSE = getBetter(foyers, p, cercloidRayon, pointSE.withModifiedCoords(MODIFIED_COORDS[0]), pointSE.withModifiedCoords(MODIFIED_COORDS[1]), pointSE.withModifiedCoords(MODIFIED_COORDS[2]));
}
}
else
{
throw new IllegalArgumentException("P ne peut être négatif en realtime");
}
/**
* String convention : "a"+45 == absolu de 45 "r"+142 == relatif de 12
* "d"+0 = valeur par defaut "n"+0 = pas de croix
*/
int pxMax = 0;
boolean doCross = true;
switch(foyCrossConvention)
{
case 'd':
pxMax = (int)((2.0 * (double)height) / 500.0);
break;
case 'a':
pxMax = foyCrossSize;
break;
case 'r':
pxMax = (int)(((double)foyCrossSize * (double)height) / 500.0);
break;
case 'n':
pxMax = (int)((2.0 * (double)height) / 500.0);
doCross = false;
break;
default:
break;
}
if(doCross)
{
for(Point f : foyers)
{
int x = f.getCoord(1);
int y = f.getCoord(2);
g.setColor(foyColor);
for(int i = 1; i <= pxMax; i++)
{
try
{
drawPixel(g,x - i, y);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Drawing cross out of bounds");
}
try
{
drawPixel(g,x, y - i);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Drawing cross out of bounds");
}
try
{
drawPixel(g,x + i, y);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Drawing cross out of bounds");
}
try
{
drawPixel(g,x, y + i);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Drawing cross out of bounds");
}
drawPixel(g,x, y);
}
}
}
System.out.println("Done");
}
/**
* Draw the cercloid specified with arguments (smart
* algo normalerweise)
* @param g
* L'objet sur lequel dessiner
* @param foyers
* A point array representing all the centers of the cercloid
* @param p
* Value of P
* @param cercloidColor
* The color for drawing the Cercloid
* @param cercloidRayon
* The Rayon of the cercloid
* @param width
* the widtrh of the final image
* @param height
* the height of the final image
* @param foyColor
* the color of the centers
* @param foyCrossConvention
* A char representing how to draw centers
* @param foyCrossSize
* the size of the drawed centers ( if drawed)
* @param progressing
* A frame for viewing draw process : if null , a WaitFrame will
* be shown
*/
public static void createImage(Graphics g,List<Point> foyers, Double p, Color cercloidColor, long cercloidRayon, int width, int height, Color foyColor, char foyCrossConvention, int foyCrossSize)
{
//System.out.println(foyers);
if(foyers.size()==0)return;
if(p > 0.0)
{
Point barycentre = Point.findBarycentre(foyers);
Point pointNE = barycentre.clone();
Point pointNO = barycentre.clone();
Point pointSO = barycentre.clone();
Point pointSE = barycentre.clone();
double error = -1.0;
while(error < 0)
{
pointNE.incrementCoord(1);
error = TestDistance.getDistance(pointNE, foyers, p) - cercloidRayon;
}
error = -1.0;
while(error < 0)
{
pointNO.incrementCoord(2);
error = TestDistance.getDistance(pointNO, foyers, p) - cercloidRayon;
}
error = -1.0;
while(error < 0)
{
pointSO.decrementCoord(1);
error = TestDistance.getDistance(pointSO, foyers, p) - cercloidRayon;
}
error = -1.0;
while(error < 0)
{
pointSE.decrementCoord(2);
error = TestDistance.getDistance(pointSE, foyers, p) - cercloidRayon;
}
pointNE.decrementCoord(1);
pointNO.decrementCoord(2);
pointSO.incrementCoord(1);
pointSE.incrementCoord(2);
Point firstPointNE = pointNE.clone();
//System.out.println(pointNE + "," + pointNO + "," + pointSO + "," + pointSE);
// List<Point> figure = new ArrayList<>();
g.setColor(cercloidColor);
int[][] MODIFIED_COORDS = {{-1, 0}, {0, +1}, {-1, +1}};
while(pointNE.getCoord(1)>pointNO.getCoord(1))
{
// figure.add(pointNE);
//System.out.println(pointNE);
try
{
drawPixel(g,pointNE.getCoord(1), pointNE.getCoord(2));
}
catch(ArrayIndexOutOfBoundsException e)
{
System.err.println("error");
}
pointNE = getBetter(foyers, p, cercloidRayon, pointNE.withModifiedCoords(MODIFIED_COORDS[0]), pointNE.withModifiedCoords(MODIFIED_COORDS[1]), pointNE.withModifiedCoords(MODIFIED_COORDS[2]));
}
MODIFIED_COORDS = new int[][] {{-1, 0}, {0, -1}, {-1, -1}};
while(pointNO.getCoord(2)>pointSO.getCoord(2))
{
// figure.add(pointNO);
try
{
drawPixel(g,pointNO.getCoord(1), pointNO.getCoord(2));
}
catch(ArrayIndexOutOfBoundsException e)
{
System.err.println("error");
}
pointNO = getBetter(foyers, p, cercloidRayon, pointNO.withModifiedCoords(MODIFIED_COORDS[0]), pointNO.withModifiedCoords(MODIFIED_COORDS[1]), pointNO.withModifiedCoords(MODIFIED_COORDS[2]));
}
MODIFIED_COORDS = new int[][] {{+1, 0}, {0, -1}, {+1, -1}};
while(pointSO.getCoord(1)<pointSE.getCoord(1))
{
// figure.add(pointSO);
try
{
drawPixel(g,pointSO.getCoord(1), pointSO.getCoord(2));
}
catch(ArrayIndexOutOfBoundsException e)
{
System.err.println("error");
}
pointSO = getBetter(foyers, p, cercloidRayon, pointSO.withModifiedCoords(MODIFIED_COORDS[0]), pointSO.withModifiedCoords(MODIFIED_COORDS[1]), pointSO.withModifiedCoords(MODIFIED_COORDS[2]));
}
MODIFIED_COORDS = new int[][] {{+1, 0}, {0, +1}, {+1, +1}};
while(pointSE.getCoord(2)<firstPointNE.getCoord(2))
{
// figure.add(pointSE);
try
{
drawPixel(g,pointSE.getCoord(1), pointSE.getCoord(2));
}
catch(ArrayIndexOutOfBoundsException e)
{
System.err.println("error");
}
pointSE = getBetter(foyers, p, cercloidRayon, pointSE.withModifiedCoords(MODIFIED_COORDS[0]), pointSE.withModifiedCoords(MODIFIED_COORDS[1]), pointSE.withModifiedCoords(MODIFIED_COORDS[2]));
}
}
else
{
throw new IllegalArgumentException("P ne peut être négatif en realtime");
}
/**
* String convention : "a"+45 == absolu de 45 "r"+142 == relatif de 12
* "d"+0 = valeur par defaut "n"+0 = pas de croix
*/
int pxMax = 0;
boolean doCross = true;
switch(foyCrossConvention)
{
case 'd':
pxMax = (int)((2.0 * (double)height) / 500.0);
break;
case 'a':
pxMax = foyCrossSize;
break;
case 'r':
pxMax = (int)(((double)foyCrossSize * (double)height) / 500.0);
break;
case 'n':
pxMax = (int)((2.0 * (double)height) / 500.0);
doCross = false;
break;
default:
break;
}
if(doCross)
{
g.setColor(foyColor);
for(Point f : foyers)
{
int x = f.getCoord(1);
int y = f.getCoord(2);
for(int i = 1; i <= pxMax; i++)
{
try
{
drawPixel(g,x - i, y);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Drawing cross out of bounds");
}
try
{
drawPixel(g,x, y - i);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Drawing cross out of bounds");
}
try
{
drawPixel(g,x + i, y);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Drawing cross out of bounds");
}
try
{
drawPixel(g,x, y + i);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Drawing cross out of bounds");
}
drawPixel(g,x, y);
}
}
}
System.out.println("Done");
}
public static void drawPixel(Graphics g, int x, int y) {
g.drawLine(x,y,x,y);
}
public static Point getBetter(Point[] foyers, double p, long rayon, Point... points)
{
Point closerPoint = null;
double closerPointDelta = Double.POSITIVE_INFINITY;
System.out.print("{");
//System.out.print("{");
for(Point point : points)
{
double currentD = abs(TestDistance.getDistance(point, foyers, p) - (double)rayon);
System.out.print(currentD+",");
if(currentD < closerPointDelta)
//System.out.print(currentD+",");
if(currentD <= closerPointDelta)
{
closerPoint = point;
closerPointDelta = currentD;
}
}
System.out.println("}");
//System.out.println("}");
return closerPoint;
}
public static Point getBetter(List<Point> foyers, double p, long rayon, Point... points)
{
Point closerPoint = null;
double closerPointDelta = Double.POSITIVE_INFINITY;
//System.out.print("{");
for(Point point : points)
{
double currentD = abs(TestDistance.getDistance(point, foyers, p) - (double)rayon);
//System.out.print(currentD+",");
if(currentD <= closerPointDelta)
{
closerPoint = point;
closerPointDelta = currentD;
}
}
//System.out.println("}");
return closerPoint;
}
@ -711,7 +1435,7 @@ public class ImageCreator
{
RunnerFactory.openImage(path);
}
String[] items = {"Refaire avec les mêmes paramètres", "Refaire une autre image", "Fermer"};
String[] items = {"Refaire avec les m<EFBFBD>mes param<61>tres", "Refaire une autre image", "Fermer"};
int rang = JOptionPane.showOptionDialog(null, "Quoi que c'est qu'on fait maintenant", "Hein ... ?", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, items, items[2]);
switch(rang)

View File

@ -1,7 +1,7 @@
package com.bernard.cercloide.factory;
public class NumberAnalyser {
static final Character eszett = new Character((char) 255);
static final Character eszett = Character.valueOf((char) 255);
public static final Double parseDouble(String s) {
switch (s.toUpperCase()) {
@ -33,7 +33,7 @@ public class NumberAnalyser {
try {
return Double.parseDouble(s);
} catch (NumberFormatException e) {
if (s.charAt(0) == 'ß') {
if (s.charAt(0) == '<EFBFBD>') {
return Double.valueOf(4.55455);
}
System.out.println("Akeuh je connais pas " + s);
@ -56,7 +56,7 @@ public class NumberAnalyser {
try {
return Long.parseLong(s);
} catch (NumberFormatException e) {
if (s.equals(eszett)) {
if (s.equals(eszett.toString())) {
return Long.valueOf(455455);
}
System.out.println("Akeuh je connais pas l'entier" + s);

View File

@ -6,8 +6,8 @@ import com.bernard.cercloide.theory.iter.IterOptions;
import com.bernard.cercloide.theory.plan.Point;
import com.bernard.cercloide.view.LoopUtilFrame;
import com.bernard.cercloide.view.MainFrame;
import com.bernard.cercloide.view.RealtimeFrame;
import com.bernard.cercloide.view.VarSetFrame;
import com.bernard.console.Console;
//Screen : 1280/1024
@ -16,9 +16,10 @@ public class Main {
public static MainFrame mainFrame;
public static VarSetFrame varSetFrame;
public static LoopUtilFrame loopUtilFrame;
public static RealtimeFrame rtFrame;
public static void main(String[] args) {
Console.createConsole();
//Console.createConsole();
mainFrame = new MainFrame();
}
public static void main2(String[] args) {

View File

@ -1,6 +1,7 @@
package com.bernard.cercloide.theory.plan;
import java.util.Arrays;
import java.util.List;
/**
* This class represents a point defined by a coordinates tab
@ -77,6 +78,26 @@ public class Point {
}
return new Point(coords);
}
public static Point findBarycentre(List<Point> foyers) {
final int coordNumber = foyers.get(0).coords.length;
long[] coordsSum = new long[coordNumber];
Arrays.fill(coordsSum, 0L);
for (Point p : foyers) {
System.out.println(foyers.get(0));
if (p.coords.length != coordNumber) {
throw new IllegalArgumentException("Les points ne sont pas de m<>me dimension !!!");
}
for (int i = 0; i < p.getCoords().length; i++) {
coordsSum[i] += p.getCoord(i+1);
}
}
int[] coords = new int[coordsSum.length];
for (int i = 0; i < coordsSum.length; i++) {
coords[i] = (int) Math.floorDiv(coordsSum[i], foyers.size());
}
return new Point(coords);
}
public Point incrementCoord(int i) {
coords[i-1]++;

View File

@ -14,12 +14,12 @@ public class MainFrame extends JFrame
private static final long serialVersionUID = 1814260752002876933L;
JButton singleImageButton, loopButton, quitButton;
JButton singleImageButton, realtimeButton, loopButton, quitButton;
GlobalListener globalListener = new GlobalListener();
public MainFrame()
{
this.setTitle("Cercloïdator");
this.setTitle("Cercloidator");
this.setSize(500, 500);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
@ -31,8 +31,10 @@ public class MainFrame extends JFrame
private void init()
{
singleImageButton = new JButton("Générateur de cercloids");
singleImageButton = new JButton("Générateur de cercloids");
singleImageButton.addActionListener(globalListener);
realtimeButton = new JButton("Cercloïdes en temps réel");
realtimeButton.addActionListener(globalListener);
loopButton = new JButton("Ouvrir le loopUtil");
loopButton.addActionListener((e) ->
{
@ -49,11 +51,12 @@ public class MainFrame extends JFrame
quitButton = new JButton("Quitter");
quitButton.addActionListener((e) -> System.exit(0));
GridLayout gl = new GridLayout(3, 1);
GridLayout gl = new GridLayout(4, 1);
gl.setHgap(20);
gl.setVgap(40);
this.getContentPane().setLayout(gl);
this.getContentPane().add(singleImageButton);
this.getContentPane().add(realtimeButton);
this.getContentPane().add(loopButton);
this.getContentPane().add(quitButton);
}
@ -75,10 +78,20 @@ public class MainFrame extends JFrame
});
t.start();
setVisible(false);
}else if(e.getSource() == realtimeButton){
Thread t = new Thread(new Runnable()
{
public void run()
{
Main.rtFrame = new RealtimeFrame();
}
});
t.start();
setVisible(false);
}
else
{
System.err.println("ERROR : Qqn à déclenché l'actionPerformed mais il a pas été catché");
System.err.println("ERROR : Qqn a déclenché l'actionPerformed mais il a pas été catch<63>");
}
}

View File

@ -0,0 +1,237 @@
package com.bernard.cercloide.view;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class RealtimeFrame extends JFrame {
private static final long serialVersionUID = -924337290982115656L;
public static final Color DEFAULT_CERCLOID_COLOR = Color.GREEN;
public static final double WHEEL_MODIF_RATE = 0.05;// 1%
GlobalListener glist;
JPanel configPanel;
RealtimePanel rtPanel;
JButton setImageButton,removeLastFoyButton,resetFoysButton;
JTextField pValue,rValue,fValue;
JColorChooser cchoose;
JLabel errorMessage;
public static final int DEFAULT_RAYON = 100;
public static final double DEFAULT_P = 2.;
public static final double DEFAULT_F = 3.;
public RealtimeFrame() {
this.setSize(900, 600);
this.setLocationRelativeTo(null);
this.setResizable(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Dessin en temps réel");
init();
this.setVisible(true);
}
private void init()
{
glist = new GlobalListener();
rtPanel = new RealtimePanel(DEFAULT_RAYON,DEFAULT_P,DEFAULT_F);
configPanel = new JPanel();
configPanel.setLayout(new BoxLayout(configPanel,BoxLayout.PAGE_AXIS));
setImageButton = new JButton("Changer l'image de fond");
setImageButton.addActionListener(glist);
JLabel pValText = new JLabel("p : ");
pValue = new JTextField(Double.toString(DEFAULT_P));
pValText.setLabelFor(pValue);
JLabel rValText = new JLabel("Rayon : ");
rValue = new JTextField(Integer.toString(DEFAULT_RAYON));
rValText.setLabelFor(rValue);
JLabel fValText = new JLabel("Épaisseur : ");
fValue = new JTextField(Double.toString(DEFAULT_F));
fValText.setLabelFor(fValue);
pValue.addCaretListener(glist);
rValue.addCaretListener(glist);
fValue.addCaretListener(glist);
pValue.addMouseWheelListener(glist);
rValue.addMouseWheelListener(glist);
fValue.addMouseWheelListener(glist);
cchoose = new JColorChooser(DEFAULT_CERCLOID_COLOR);
cchoose.getSelectionModel().addChangeListener(glist);;
errorMessage = new JLabel(" ");
errorMessage.setBackground(Color.YELLOW);
errorMessage.setForeground(Color.RED);
configPanel.add(setImageButton);
configPanel.add(pValue);
configPanel.add(rValue);
configPanel.add(fValue);
configPanel.add(cchoose);
configPanel.add(errorMessage);
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(configPanel, BorderLayout.EAST);
this.getContentPane().add(rtPanel, BorderLayout.CENTER);
}
public class GlobalListener implements ActionListener,CaretListener, ChangeListener, MouseWheelListener{
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == setImageButton) {
JFileChooser jfc = new JFileChooser();
int resultat = jfc.showOpenDialog(RealtimeFrame.this);
if(resultat == JFileChooser.APPROVE_OPTION) {
File toRead = jfc.getSelectedFile();
try {
Image img = ImageIO.read(toRead);
if(img==null) {
JOptionPane.showMessageDialog(RealtimeFrame.this, "Impossible de lire le fichier spécifié, est-ce vraiment une image ?", "Erreur de lecture", JOptionPane.ERROR_MESSAGE);
}else {
rtPanel.setImg(img);
rtPanel.repaint();
}
} catch (IOException e1) {
e1.printStackTrace();
JOptionPane.showMessageDialog(RealtimeFrame.this, "Impossible de lire le fichier spécifié", "Erreur de lecture", JOptionPane.ERROR_MESSAGE);
}
}
}
}
@Override
public void caretUpdate(CaretEvent e) {
System.out.println(e);
if(e.getSource()==pValue) {
try {
Double p = Double.parseDouble(pValue.getText());
if(p>0. && Double.isFinite(p)) {
errorMessage.setText(" ");
pValue.setBackground(Color.WHITE);
rtPanel.setP(p);
rtPanel.repaint();
}else {
throw new NumberFormatException();
}
}catch (NumberFormatException exp) {
//JOptionPane.showMessageDialog(RealtimeFrame.this, "Votre valeur de p est incorrecte, ce doit être un flottant positif fini", "Mauvaise entrée", JOptionPane.ERROR_MESSAGE);
errorMessage.setText("Votre valeur de p est incorrecte, ce doit être un flottant positif fini");
pValue.setBackground(Color.ORANGE);
}
}else if(e.getSource()==rValue) {
try {
Integer r = Integer.parseInt(rValue.getText());
if(r>0) {
errorMessage.setText(" ");
rValue.setBackground(Color.WHITE);
rtPanel.setCercloidRayon(r);
rtPanel.repaint();
}else {
throw new NumberFormatException();
}
}catch (NumberFormatException exp) {
//JOptionPane.showMessageDialog(RealtimeFrame.this, "Votre valeur de rayon est incorrecte, ce doit être un entier positif", "Mauvaise entrée", JOptionPane.ERROR_MESSAGE);
errorMessage.setText("Votre valeur de rayon est incorrecte, ce doit être un entier positif");
rValue.setBackground(Color.ORANGE);
}
}else if(e.getSource()==fValue) {
try {
Double f = Double.parseDouble(fValue.getText());
if(f>0. && Double.isFinite(f)) {
errorMessage.setText(" ");
fValue.setBackground(Color.WHITE);
rtPanel.setFourchette(f);
rtPanel.repaint();
}else {
throw new NumberFormatException();
}
}catch (NumberFormatException exp) {
//JOptionPane.showMessageDialog(RealtimeFrame.this, "Votre valeur de p est incorrecte, ce doit être un flottant positif fini", "Mauvaise entrée", JOptionPane.ERROR_MESSAGE);
errorMessage.setText("Votre valeur de l'épaisseur est incorrecte, ce doit être un flottant positif fini");
fValue.setBackground(Color.ORANGE);
}
}
}
@Override
public void stateChanged(ChangeEvent e) {
if(e.getSource()==cchoose.getSelectionModel()) {
rtPanel.setCercloidColor(cchoose.getColor());
rtPanel.repaint();
}
}
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
if(e.getComponent()==rValue) {
int r = rtPanel.getCercloidRayon();
double ts = - e.getPreciseWheelRotation();
r += (int)(ts*r*WHEEL_MODIF_RATE);
if(r>0) {
rValue.setText(Integer.toString(r));
rtPanel.setCercloidRayon(r);
rtPanel.repaint();
}
}else if(e.getComponent()==pValue) {
double p = rtPanel.getP();
double ts = - e.getPreciseWheelRotation();
p += ts*p*WHEEL_MODIF_RATE;
if(p>0. && Double.isFinite(p)) {
pValue.setText(Double.toString(p));
rtPanel.setP(p);
rtPanel.repaint();
}
}else if(e.getComponent()==fValue) {
double f = rtPanel.getFourchette();
double ts = - e.getPreciseWheelRotation();
f += ts*f*WHEEL_MODIF_RATE;
if(f>0. && Double.isFinite(f)) {
fValue.setText(Double.toString(f));
rtPanel.setFourchette(f);
rtPanel.repaint();
}
}
}
}
}

View File

@ -0,0 +1,280 @@
package com.bernard.cercloide.view;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JPanel;
import com.bernard.cercloide.factory.ImageCreator;
import com.bernard.cercloide.theory.plan.Point;
public class RealtimePanel extends JPanel {
private static final long serialVersionUID = 8221004514247316046L;
Image img;
double p;
double fourchette;
List<Point> foyers = new ArrayList<>();
Color cercloidColor = RealtimeFrame.DEFAULT_CERCLOID_COLOR, foyColor = Color.RED;
int cercloidRayon;
char foyCrossConv = 'd';
int foyCrossSize = 6;
Draggeur dragueur;
public RealtimePanel(int rayon,double p,double fourchette) {
this.cercloidRayon = rayon;
this.p=p;
this.fourchette = fourchette;
this.dragueur = new Draggeur();
this.addMouseListener(dragueur);
this.addMouseMotionListener(dragueur);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int w=getWidth(),h=getHeight();
/*
g.setColor(Color.CYAN);
g.drawRect(0,0, w, h);
Point[] foyersArr = new Point[foyers.size()];
foyers.toArray(foyersArr);
img = ImageCreator.createImage(foyersArr, p, cercloidColor, cercloidRayon, 3, h,w, foyColor, 'n', 0);
*/
if(img==null) {
g.setColor(Color.BLACK);
}else {
int iw=img.getHeight(null),ih=img.getWidth(null);
int rw,rh;
if(iw<=w && ih<=h) {
rw=iw;
rh=ih;
}else {
if(h*iw<=ih*w) {
rw=h*iw/ih;
rh=h;
}else {
rw=w;
rh=w*ih/iw;
}
}
g.drawImage(img, w/2-rw/2, h/2-rh/2, rw, rh, null);
}
Point[] foyersArr = new Point[foyers.size()];
foyers.toArray(foyersArr);
ImageCreator.createImage(g, foyersArr, p, cercloidColor, cercloidRayon, fourchette,w,h , foyColor, foyCrossConv, foyCrossSize);
}
public class Draggeur implements MouseListener,MouseMotionListener{
int draggedPoint = -1;
@Override
public void mouseClicked(MouseEvent e) {
if(isIn(e)) {
if(e.getButton()==MouseEvent.BUTTON1) {
Point p = new Point(e.getX(),e.getY());
foyers.add(p);
repaint();
}else if(e.getButton()==MouseEvent.BUTTON3) {
int selected = getSelection(e);
if(selected!=-1) {
if(selected==draggedPoint)draggedPoint=-1;
if(selected<draggedPoint)draggedPoint--;
foyers.remove(selected);
repaint();
}
}
}
System.out.println("Nouveaux Foyers"+e.getButton()+foyers);
}
@Override
public void mousePressed(MouseEvent e) {
draggedPoint = getSelection(e);
}
@Override
public void mouseReleased(MouseEvent e) {
draggedPoint = -1;
repaint();
}
@Override
public void mouseEntered(MouseEvent e) {
// Inutilisé
}
@Override
public void mouseExited(MouseEvent e) {
// Inutilisé
}
@Override
public void mouseDragged(MouseEvent e) {
if(draggedPoint!=-1) {
int x=e.getX(),y=e.getY();
Point nP = new Point(x,y);
foyers.set(draggedPoint, nP);
repaint();
}
}
@Override
public void mouseMoved(MouseEvent e) {
// Inutilisé
}
private boolean isIn(MouseEvent e) {
return e.getX() < getWidth() && e.getY() < getHeight() && e.getX()>=0 && e.getY()>=0;
}
public static final int THRESHOLD_MOUSE_GRAB_DISTANCE = 10*10;
private int getSelection(MouseEvent e) {
int x=e.getX(),y=e.getY();
int minD=Integer.MAX_VALUE;
int selection = -1;
for(int i = 0;i<foyers.size();i++) {
int d = carre(foyers.get(i).getCoord(1)-x)+carre(foyers.get(i).getCoord(2)-y);
if(d<minD) {
selection = i;
minD=d;
}
}
System.out.println(selection+"<-->"+minD);
return (minD<=THRESHOLD_MOUSE_GRAB_DISTANCE)?selection:-1;
}
private int carre(int i) {return i*i;}
}
public Image getImg() {
return img;
}
public void setImg(Image img) {
this.img = img;
}
public double getP() {
return p;
}
public void setP(double p) {
this.p = p;
}
public List<Point> getFoyers() {
return foyers;
}
public void setFoyers(List<Point> foyers) {
this.foyers = foyers;
}
public void addFoyer(Point foyer) {
this.foyers.add(foyer);
}
public Color getCercloidColor() {
return cercloidColor;
}
public void setCercloidColor(Color cercloidColor) {
this.cercloidColor = cercloidColor;
}
public Color getFoyColor() {
return foyColor;
}
public void setFoyColor(Color foyColor) {
this.foyColor = foyColor;
}
public int getCercloidRayon() {
return cercloidRayon;
}
public void setCercloidRayon(int cercloidRayon) {
this.cercloidRayon = cercloidRayon;
}
public char getFoyCrossConv() {
return foyCrossConv;
}
public void setFoyCrossConv(char foyCrossConv) {
this.foyCrossConv = foyCrossConv;
}
public int getFoyCrossSize() {
return foyCrossSize;
}
public void setFoyCrossSize(int foyCrossSize) {
this.foyCrossSize = foyCrossSize;
}
public double getFourchette() {
return fourchette;
}
public void setFourchette(double fourchette) {
this.fourchette = fourchette;
}
}