How to put different color random dots in a rectangle separated by a diagonal (JAVA) -


my problem cant diagonal resize properly. if run code diagonal on wrong side , when expand vertically doesn't maintain perfect diagonal 1 corner another.

below have code program , driver. import javax.swing.jframe;

public class points {  public static void main(string[] args) {   jframe frame = new jframe("points");   frame.setdefaultcloseoperation(jframe.exit_on_close);    pointspanel panel = new pointspanel();    frame.getcontentpane().add(panel);   frame.pack();   frame.setvisible(true);   }   } 

here main program

import javax.swing.jpanel; import java.awt.dimension; import java.awt.graphics; import java.util.random; import java.awt.color; import java.lang.math.*;  public class pointspanel extends jpanel {  private final int max_points = 20000;  private final int length = 1;  private int x,x1,y,y1;  private random random;  double slope,b,c,g;    public pointspanel(){   random = new random();   setbackground(color.black);   setpreferredsize(new dimension(300,300));   }    public void paintcomponent(graphics page)  {   super.paintcomponent(page);  for(int count = 0; count < max_points; count++)  {    x = random.nextint(getwidth()-1) + 1;    y = random.nextint(getheight()-1) + 1;    x1= x + length;   y1= y + length;    slope = x1/y1;     c = slope*x;     b = ((-1)*(y))-c;     g = (-1)*y1;    if (b <= g)       page.setcolor(color.red);   else      page.setcolor(color.green);      page.drawline(x,y,x1,y1);  }   }  } 

i solved issue making little change pointspanel class. take @ pointspanel2 class. first of diagonal linear equation of form y = ax + b , since passes points of coordinate (0,0) can reduce y = ax therefore = y/x (x,y) point part of diagonal.

now event though drawing methods use integer coordinate we still have compute coefficient floating number (like double), otherwise won't work because of rounding issues.

then once other part of code runs , obtain (x,y) x1 , y1, have compute corresponding diagonal y x1 , compare y1 , depending on obtained value, change color red or green

use pointspanel2 class instead of pointspanel test other points class

import javax.swing.jpanel; import java.awt.dimension; import java.awt.graphics; import java.util.random; import java.awt.color; import java.lang.math.*;  public class pointspanel2 extends jpanel {      private final int max_points = 20000;      private final int length = 1;      private int x,x1,y,y1;      private random random;      double slope,b,c,g;        public pointspanel2(){      random = new random();      setbackground(color.black);      setpreferredsize(new dimension(300,300));   }    public void paintcomponent(graphics page)  {       super.paintcomponent(page);       // diagonal equation       // x0, y0 = (0,0)       // xl, yl = width, height       // equation y = ax + b       // 0 = 0 + b ==> b = 0       // yl = axl ==> = yl/xl        // computes diagonal coefficient       double acoeff = (getheight()*1.0)/(getwidth()*1.0);       system.out.println("xl,yl, = " + getwidth() + " " + getheight() + "   " + acoeff);        for(int count = 0; count < max_points; count++)       {            x = random.nextint(getwidth()-1) + 1;            y = random.nextint(getheight()-1) + 1;            x1= x + length;           y1= y + length;            // computes diagonal image of x in order compare y1           double diagimage = acoeff * x1;            slope = x1/y1;           c = slope*x;           b = ((-1)*(y))-c;           g = (-1)*y1;            //if (b <= g)           if (diagimage <= y1)               page.setcolor(color.red);           else               page.setcolor(color.green);           page.drawline(x,y,x1,y1);         }      }  } 

Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

python - NameError: name 'subprocess' is not defined -