TP03 ; graphsup proto

This commit is contained in:
HORVILLE 2022-10-20 12:49:09 +02:00
parent a2f0837add
commit 714c782053
11 changed files with 501 additions and 23 deletions

View File

@ -44,7 +44,7 @@ int main(void) {
int level;
/*printf("Niveau de complexité : ");
scanf("%d", &level);*/
level = 14;
level = 12;
vec2 vec = {WIDTH/2, 200};
double a = 0;

View File

@ -0,0 +1,24 @@
/**
* ChainLink
*/
public class ChainLink<T> {
private T value;
private T next;
public T getValue() {
return value;
}
public void setValue(T value) {
this.value = value;
}
public T getNext() {
return next;
}
public void setNext(T next) {
this.next = next;
}
}

View File

@ -0,0 +1,3 @@
public class ListChain {
}

View File

@ -0,0 +1,36 @@
import java.awt.event.*;
public class LumListener implements MouseListener {
@Override
public void mouseClicked(MouseEvent e) {
LumPanel p = (LumPanel)e.getSource();
p.removeParralel(e.getX(), e.getY());
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}

View File

@ -0,0 +1,72 @@
import javax.swing.JPanel;
import java.awt.*;
import java.util.LinkedList;
import java.util.Random;
public class LumPanel extends JPanel {
private LinkedList<Color> l;
int width = 30;
int offset = 30;
int height = 60;
int spacing = 5;
public LumPanel() {
l = new LinkedList<Color>();
Random r = new Random();
for (int i = 0; i < 10; i++) {
l.add(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)));
}
}
private double lerp(double a, double b, double t) {
return a + (b - a) * t;
}
public void removeParralel(int x, int y) {
double dx = (double)x;
double dy = (double)y;
dx -= lerp(offset, 0, dy / (double)height);
dx -= spacing * Math.floor(dx / width);
int index = (int)Math.floor(dx / width);
if (index >= 0 && index < l.size()) {
Color c = l.get(index);
System.out.println("Luminance : " + (21 * c.getRed() + 72 * c.getGreen() + 7 * c.getBlue()));
l.remove(index);
repaint();
}
}
@Override
protected void paintComponent(Graphics g) {
Graphics gg = g.create();
if (isOpaque()) {
gg.setColor(getBackground());
gg.fillRect(getX(), getY(), getWidth(), getHeight());
}
int i = 0;
for (Color c : l) {
Polygon p = new Polygon();
int x = getX() + spacing * i + width * i;
p.addPoint(x + offset, getY());
p.addPoint(x + offset + width, getY());
p.addPoint(x + width, getY() + height);
p.addPoint(x, getY() + height);
gg.setColor(c);
gg.fillPolygon(p);
gg.setColor(Color.BLACK);
gg.drawPolygon(p);
i++;
}
}
}

View File

@ -0,0 +1,27 @@
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.*;
/**
* Luminance
*/
public class Luminance {
public static void main(String[] args) {
JFrame f = new JFrame();
LumPanel lm = new LumPanel();
lm.setLocation(0, 0);
lm.setSize(480, 100);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocation(200, 200);
f.setSize(400, 100);
f.setResizable(false);
f.add(lm);
f.setLayout(null);
f.setVisible(true);
lm.addMouseListener(new LumListener());
}
}

View File

@ -6,6 +6,8 @@ import javax.swing.JFrame;
*/
public class Fun {
private static long time;
public static void main(String[] args) {
JFrame frame = new JFrame();
@ -17,14 +19,14 @@ public class Fun {
frame.setVisible(true);
time = System.nanoTime();
while (true) {
FunPanel.table += 0.001d;
if (System.nanoTime() - time > 16666666L / 10L) {
FunPanel.table += 0.0001d;
p.repaint();
try {
Thread.sleep(16L);
} catch (Exception e) {
time = System.nanoTime();
}
}
}

View File

@ -1,14 +1,26 @@
import javax.swing.JPanel;
import java.awt.*;
import java.util.Random;
public class FunPanel extends JPanel {
public static double table = 50;
public static int limit = 30;
public static double table = 0;
public static int limit = 150;
public FunPanel() {
super();
}
private static Point getCirclePoint(double angle, double radius, Point offset) {
Point p = new Point();
p.x = (int)(Math.cos(angle) * radius) + offset.x;
p.y = (int)(Math.sin(angle) * radius) + offset.y;
return p;
}
private static float n = 0;
private static Random r = new Random();
@Override
protected void paintComponent(Graphics g) {
Graphics gg = g.create();
@ -19,23 +31,22 @@ public class FunPanel extends JPanel {
}
double size = (double)(getWidth() > getHeight() ? getHeight() : getWidth()) / 2.5d;
Point offset = new Point(getX() + getWidth() / 2, getY() + getHeight() / 2);
gg.setColor(Color.BLACK);
for (double i = 0; i < limit; i++) {
double theta = (i / (double)limit) * Math.PI * 2;
gg.drawString("" + table, 10, 15);
gg.fillOval((int)(Math.cos(theta) * size) + getWidth() / 2, (int)(Math.sin(theta) * size) + getHeight() / 2, 6, 6);
for (double i = 1; i <= limit; i++) {
double theta = (i / (double)limit) * Math.PI * 2 - Math.PI / 2 - (Math.PI * 2 / (double)limit);
Point p0 = getCirclePoint(theta, size, offset);
for (double j = 0; j < limit; j++) {
double theta2 = (table * j / (double)limit) * Math.PI * 2;
//gg.fillOval(p0.x, p0.y, 6, 6);
//gg.drawString("" + i, p0.x, p0.y);
int x1 = (int)(Math.cos(theta) * size) + getWidth() / 2;
int x2 = (int)(Math.cos(theta2) * size) + getWidth() / 2;
int y1 = (int)(Math.sin(theta) * size) + getHeight() / 2;
int y2 = (int)(Math.sin(theta2) * size) + getHeight() / 2;
gg.drawLine(x1 + 3, y1 + 3, x2 + 3, y2 + 3);
for (double j = 1; j <= limit; j++) {
double theta2 = (table * i / (double)limit) * Math.PI * 2 - Math.PI / 2 - (Math.PI * 2 / (double)limit);
Point p1 = getCirclePoint(theta2, size, offset);
gg.drawLine(p0.x, p0.y, p1.x, p1.y);
}
}
}

256
graphsup/graphsup.c Normal file
View File

@ -0,0 +1,256 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <graph.h>
#include "graphsup.h"
//----------------------------- Framerate -----------------------------//
#define DEFAULT_FRAMERATE 60.0
//Des variables afin de faciliter le calcul du temps entre chaque image pour int nextFrame().
double targetFramerate = DEFAULT_FRAMERATE;
double delta = (1/DEFAULT_FRAMERATE) * 1000000;
unsigned long suivant = (1/DEFAULT_FRAMERATE) * 1000000;
//Ajuste la fréquence de rafraichissement donnée par nextFrame().
void setFramerate(int framerate) {
targetFramerate = (double)framerate;
delta = (1/targetFramerate) * 1000000;
suivant = Microsecondes() + delta;
}
//Permet de faire tourner la partie graphique à X images par seconde définit par targetFramerate et setFramerate.
int nextFrame() {
unsigned long t = Microsecondes();
if (t >= suivant) {
suivant = t + delta;
return 1;
}
return 0;
}
//------------------------------ Couleurs ------------------------------//
//Retourne une couleur suivant son nom.
couleur getColorN(char* name) {
return CouleurParNom(name);
}
//Retourne une couleur suivant son code RGB.
couleur getColor(unsigned char r, unsigned char g, unsigned char b) {
return CouleurParComposante(r, g, b);
}
//Définit la couleur des dessins à suivre suivant un code RGB.
void setColor(unsigned char r, unsigned char g, unsigned char b) {
ChoisirCouleurDessin(GetColor(r, g, b));
}
//Définit la couleur des dessins à suivre suivant la couleur donnée.
void setColorC(couleur Color) {
ChoisirCouleurDessin(Color);
}
//Définit la couleur des dessins à suivre suivant le nom donné.
void setColorN(char* name) {
ChoisirCouleurDessin(GetColorN(name));
}
couleur getHSVColor(double h, double s, double v) {
double c = v * s;
double m = v - c;
double r, g, b;
h -= (double)(int)h;
if (h <= 1.0 / 6.0) {
r = 255.0;
g = h * 6.0 * 255.0;
b = 0.0;
} else if (h <= 2.0 / 6.0) {
r = (1.0 - (h - 1.0 / 6.0) * 6.0) * 255.0;
g = 255.0;
b = 0.0;
} else if (h <= 3.0 / 6.0) {
r = 0.0;
g = 255.0;
b = (h - 1.0 / 6.0) * 6.0 * 255.0;
} else if (h <= 4.0 / 6.0) {
r = 0.0;
g = (1.0 - (h - 0.5) * 6.0) * 255.0;
b = 255.0;
} else if (h <= 5.0 / 6.0) {
r = (h - 4.0 / 6.0) * 6.0 * 255.0;
g = 0.0;
b = 255.0;
} else {
r = 255.0;
g = 0.0;
b = (1.0 - (h - 5.0 / 6.0) * 6.0) * 255.0;
}
r += m;
g += m;
b += m;
return CouleurParComposante(r, g, b);
}
//------------------------------ Dessins ------------------------------//
void drawRegPoly(int x, int y, int s, double r, double a) {
double delta = M_PI * 2.0 / s;
for (int i = 0; i < s; i++) {
int x1 = (int)(cos(delta * i + a) * r + x);
int y1 = (int)(sin(delta * i + a) * r + y);
int x2 = (int)(cos(delta * (i+1) + a) * r + x);
int y2 = (int)(sin(delta * (i+2) + a) * r + y);
DessinerSegment(x1, y1, x2, y2);
}
}
void fillRegPoly(int x, int y, int s, double r, double a) {
double delta = M_PI * 2.0 / s;
for (int i = 0; i < s; i++) {
int x1 = (int)(cos(delta * i + a) * r + x);
int y1 = (int)(sin(delta * i + a) * r + y);
int x2 = (int)(cos(delta * (i+1) + a) * r + x);
int y2 = (int)(sin(delta * (i+2) + a) * r + y);
RemplirTriangle(x, y, x1, y1, x2, y2);
}
}
//------------------------------ Boutons ------------------------------//
button* firstButton = NULL;
//Enlève tous les boutons présents
void clearButtons(void) {
if (firstButton == NULL) return;
button* bt = firstButton;
while(bt != NULL) {
button* next = bt->next;
free(bt);
bt = next;
}
}
//Retourne un bouton suivant son identifiant
button* getButton(char* index) {
button* bt = firstButton;
while (bt != NULL) {
char* btIndex;
if (bt->type == RECT_BUTTON) btIndex = ((rectButton*)bt)->id;
else if (bt-> type == CIRCLE_BUTTON) btIndex = ((circleButton*)bt)->id;
else {
printf("Button type error.");
exit(1);
}
if (!strcmp(index, btIndex)) {
return bt;
}
bt = bt->next;
}
}
//Retourne un bouton rectangulaire suivant son identifiant
rectButton* getRectButton(char* index) {
button* bt = firstButton;
while (bt != NULL) {
char* btIndex;
if (bt->type == RECT_BUTTON) {
btIndex = ((rectButton*)bt)->id;
if (!strcmp(index, btIndex)) {
return (rectButton*)bt->pointer;
}
}
}
}
//Retourne un bouton rectangulaire ou NULL si le bouton donné n'est pas un bouton rectangulaire.
rectButton* toRectButton(button* bt) {
if (bt->type != RECT_BUTTON) return NULL;
return (rectButton*)bt->pointer;
}
//Crée un bouton rectangulaire et en retourne le pointeur.
rectButton* addRectButton(int x, int y, int w, int h, char* id) {
rectButton* rbt = (rectButton*)malloc(sizeof(rectButton));
rbt->x = x;
rbt->y = y;
rbt->w = w;
rbt->h = h;
rbt->id = id;
button* bt = (button*)malloc(sizeof(button));
bt->type = RECT_BUTTON;
bt->pointer = (void*)rbt;
bt->next = NULL;
button* buttonChain = firstButton;
while (buttonChain->next != NULL) buttonChain = buttonChain->next;
buttonChain->next = bt;
return rbt;
}
//Retourne un bouton rectangulaire suivant son identifiant
rectButton* getCircleButton(char* index) {
button* bt = firstButton;
while (bt != NULL) {
char* btIndex;
if (bt->type == CIRCLE_BUTTON) {
btIndex = ((circleButton*)bt)->id;
if (!strcmp(index, btIndex)) {
return (circleButton*)bt->pointer;
}
}
}
}
//Retourne un bouton circulaire ou NULL si le bouton donné n'est pas un bouton circulaire.
circleButton* toCircleButton(button* bt) {
if (bt->type != CIRCLE_BUTTON) return NULL;
return (circleButton*)bt->pointer;
}
//Crée un bouton rectangulaire et en retourne le pointeur.
circleButton* addCircleButton(int x, int y, int r, char* id) {
circleButton* rbt = (circleButton*)malloc(sizeof(circleButton));
rbt->x = x;
rbt->y = y;
rbt->r = r;
rbt->id = id;
button* bt = (button*)malloc(sizeof(button));
bt->type = CIRCLE_BUTTON;
bt->pointer = (void*)rbt;
bt->next = NULL;
button* buttonChain = firstButton;
while (buttonChain->next != NULL) buttonChain = buttonChain->next;
buttonChain->next = bt;
return rbt;
}

42
graphsup/graphsup.h Normal file
View File

@ -0,0 +1,42 @@
#ifndef _GRAPHSUP_H
#define _GRAPHSUP_H
#define RECT_BUTTON 0
#define CIRCLE_BUTTON 1
//Représente un bouton rectangulaire.
struct rectButton {
int x;
int y;
int w;
int h;
char* id;
};
//Représente un bouton circulaire.
struct circleButton {
int x;
int y;
int r;
char* id;
};
//Représente un bouton
struct button {
void* pointer;
unsigned char type;
button* next;
};
typedef struct rectButton rectButton;
typedef struct circleButton circleButton;
typedef struct button button;
void setFramerate(int framerate);
int nextFrame(void);
couleur getColorN(char* name);
couleur getColor(unsigned char r, unsigned char g, unsigned char b);
#endif

5
graphsup/test.c Normal file
View File

@ -0,0 +1,5 @@
#include <stdio.h>
#include <stdlib.h>
#include <graph.h>
#include "graphsup.h"