Afficher fonction polynomiale en JAVA

varfendell Messages postés 3259 Date d'inscription   Statut Membre Dernière intervention   -  
varfendell Messages postés 3259 Date d'inscription   Statut Membre Dernière intervention   -
Bonjour,

J'ai effectuer hier soir un algorithme permettant d'effectuer un lissage polynomiale à partir de point rentré par l'utilisateur.

J'ai pour résultat, les coefficient A0, A1, A2, ..., Ap de la fonction f(x) = A0 + A1 x + A2 x^2 + ... + Ap x^p.

Je cherche donc maintenant a représenter graphiquement cette fonction...et je ne sait comment faire.

J'avais trouvé hier soir un code qui m'avait intéressé sur CCM, mais je ne le retrouve plus :(

Merci de bien vouloir m'éclairer :)
--
La vérité appartient à ceux qui la cherchent et non point à ceux qui prétendent la détenir.
(n'oubliez pas de mettre résolu si vous avez trouvé votre   bonheur: pensez aux autres^^')
Configuration: windows viste, firefox

2 réponses

  1. Utilisateur anonyme
     
    Salut,

    Tu trouveras ci-dessous le code qui te permet d'afficher la fonction.

    Je te laisse le soin d'implémenter toi-même le calcul des facteurs (générés aléatoirement dans le code ci-dessous) grâce à ton algorithme

    ;-)
    HackTrack

    package be.ccm.polynomial;
    
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.BorderFactory;
    import javax.swing.DefaultListModel;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JList;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextField;
    
    public class Polynomial extends JFrame {
    	private static final long serialVersionUID = 1L;
    	private Container c;
    	private InputPanel inputPanel;
    	private PolynomialViewPanel view;
    
    	public Polynomial() {
    		super("Polynomial view - HackTrack 2010");
    		this.initialize();
    	}
    
    	private void initialize() {
    		c = this.getContentPane();
    		c.setLayout(new BorderLayout());
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		inputPanel = new InputPanel();
    		view = new PolynomialViewPanel();
    		c.add(inputPanel, BorderLayout.NORTH);
    		c.add(view, BorderLayout.CENTER);
    	}
    
    	class InputPanel extends JPanel implements ActionListener {
    		private static final long serialVersionUID = 1L;
    		private JTextField pointX;
    		private JTextField pointY;
    		private JTextField factorCount;
    		private JTextField xMin;
    		private JTextField xMax;
    		private JList factors;
    		private JButton okButton;
    		private JButton cancelButton;
    
    		public InputPanel() {
    			super();
    			this.initialize();
    		}
    
    		private void initialize() {
    			pointX = new JTextField(3);
    			pointY = new JTextField(3);
    			factorCount = new JTextField(2);
    			xMin = new JTextField(5);
    			xMax = new JTextField(5);
    			factors = new JList(new DefaultListModel());
    			factors.setPreferredSize(new Dimension(200,480));
    			okButton = new ActionButton("Ok", this);
    			cancelButton = new ActionButton("Cancel", new ActionListener() {
    				public void actionPerformed(ActionEvent e) {
    					System.exit(0);
    				}
    			});
    			this.add(new JLabel("x:"));
    			this.add(pointX);
    			this.add(new JLabel("y:"));
    			this.add(pointY);
    			this.add(new JLabel("Nombre de facteurs:"));
    			this.add(factorCount);
    			this.add(new JLabel("xMin:"));
    			this.add(xMin);
    			this.add(new JLabel("xMax:"));
    			this.add(xMax);
    			this.add(okButton);
    			this.add(cancelButton);
    			JPanel factorListPanel = new JPanel();
    			factorListPanel.setLayout(new BorderLayout());
    			factorListPanel.setBorder(BorderFactory.createTitledBorder("Liste des facteurs"));
    			factorListPanel.add(new JScrollPane(factors));
    			c.add(factorListPanel,BorderLayout.EAST);
    		}
    
    		public void actionPerformed(ActionEvent e) {
    			int x = Integer.parseInt(pointX.getText());
    			int y = Integer.parseInt(pointX.getText());
    			double[] factorsValue = calculateFactors(x,y);
    			DefaultListModel listModel = new DefaultListModel();
    			for (int i = 0; i < factorsValue.length; i++) {
    				listModel.add(i, "A"+i+"="+factorsValue[i]);
    			}
    			factors.setModel(listModel);
    			double xMinDouble = Double.parseDouble(xMin.getText());
    			double xMaxDouble = Double.parseDouble(xMax.getText());
    			view.drawPoly(factorsValue, xMinDouble, xMaxDouble);
    		}
    
    		/*
    		 * Tu dois modifier l'implémentation de cette méthode pour effectuer le
    		 * calcul des facteurs sur base de ton algorithme et en fonction du point (x,y).
    		 * 
    		 * J'ai supposé que x et y sont des entiers. Si ce n'est pas le cas,
    		 * adapte la signature de la méthode.
    		 */
    		private double[] calculateFactors(int x, int y) {
    			int factorsCount = Integer.parseInt(this.factorCount.getText());
    			double[] factorsValue = new double[factorsCount];
    			for (int i = 0; i < factorsCount; i++) {
    				factorsValue[i] = Math.random() * 10;
    			}
    			return factorsValue;
    		}
    	}
    
    	class PolynomialViewPanel extends JPanel {
    		private double[] factorsValue;
    		private double xMin;
    		private double xMax;
    		private double yMin;
    		private double yMax;
    
    		public PolynomialViewPanel() {
    			super();
    			this.initialize();
    		}
    
    		private void initialize() {
    			setPreferredSize(new Dimension(640, 480));
    		}
    
    		public void drawPoly(double[] factorsValue, double xMin, double xMax) {
    			this.xMin = xMin;
    			this.xMax = xMax;
    			yMin = Double.MAX_VALUE;
    			yMax = Double.MIN_VALUE;
    			this.factorsValue = factorsValue;
    			// Premier passage pour calculer yMin et yMax
    			for (double x = xMin; x < xMax; x += (xMax - xMin)
    					/ this.getWidth()) {
    				double fx = 0;
    				double factor = 1;
    				for (int fi = 0; fi < factorsValue.length; fi++) {
    					for (int exp = 0; exp < factorsValue[fi]; exp++) {
    						factor *= x;
    					}
    					fx += factor;
    					if (fx < yMin) {
    						yMin = fx;
    					}
    					if (fx > yMax) {
    						yMax = fx;
    					}
    				}
    			}
    			this.repaint();
    		}
    
    		public void paint(Graphics g) {
    			super.paint(g);
    			g.setColor(Color.WHITE);
    			g.fillRect(0,0, this.getWidth(), this.getHeight());
    			g.setColor(Color.RED);
    			// Second passage pour dessiner le graphique
    			System.out.println("(xMin,yMin)-(xMax,yMax)=(" + xMin + "," + yMin
    					+ "),(" + xMax + "," + yMax + ")");
    			double fx;
    			int[] previousCoordinate = new int[2];
    			for (double x = xMin; x < xMax; x += (xMax - xMin)
    					/ this.getWidth()) {
    				fx = 0;
    				double factor = 1;
    				for (int fi = 0; fi < factorsValue.length; fi++) {
    					for (int exp = 0; exp < factorsValue[fi]; exp++) {
    						factor *= x;
    					}
    					fx += factor;
    				}
    				int intX = (int) (this.getWidth() * (x - xMin) / (xMax - xMin));
    				int intY = (int) (this.getHeight() * (fx - yMin) / (yMax - yMin));
    				System.out.println("(x,y)=(" + intX + "," + intY + ")");
    				if (previousCoordinate != null) {
    					g.drawLine(previousCoordinate[0], previousCoordinate[1],
    							intX, intY);
    				}
    				previousCoordinate[0] = intX;
    				previousCoordinate[1] = intY;
    			}
    		}
    	}
    
    	class ActionButton extends JButton {
    		public ActionButton(String label, ActionListener al) {
    			super(label);
    			this.addActionListener(al);
    		}
    	}
    
    	public static void main(String[] args) {
    		Polynomial polyView = new Polynomial();
    		polyView.pack();
    		polyView.setVisible(true);
    	}
    }
    
    0
  2. varfendell Messages postés 3259 Date d'inscription   Statut Membre Dernière intervention   707
     
    Bonjour,

    Merci a toi pour ton aide tout d'abord.

    Il y a juste quelque truc que je n'ai pas compris:
    On doit entrer 5 éléments, Nombre de facteur correspond au degré du polynome - 1; et je suppose que Xmin et Xmax correspondent aux borne de l'affichages.
    Mais je ne vois pas à quoi correspondent les éléments X et Y.

    Je n'arrive pas a comprendre ton code, je ne connais pas très bien les interfaces, mais je pourrais modifier sans trop de problèmes la classe qui donne les coefficients.
    0