Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (777) Games in Android Showcase (231) games submitted by our members Games in WIP (856) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 help ! stuck with maths problem i think..[SOLVED]  (Read 2849 times) 0 Members and 1 Guest are viewing this topic.
markmistry

Junior Devvie

 « Posted 2012-07-21 18:25:35 »

hey everyone, i am stuck and rapidly running out of ideas with my maths problem, any pointers would be great..

consider the following... a screen is filled with random pixels the following code only half works and i have tried everything i can think of to get it working but i am at a loss. the pixels on the left hand side move outwards at an angle from the center of the screen,,,perfect just what i am looking for..
but the pixels on the right hand side when i alter the code do the following go in to the center...dont move at all(code on show) or dont even show

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31 `                  int startX = i;                  int startY = a;                  int dx = 0;                  int dy = 0;                  int endX=0;                  int endY=0;                                    if(startX <=400){                  dx = 400 - startX;dy = 250 - startY;                                                      double distToTarget = Math.sqrt(dx * dx + dy * dy);                  double ratio = 5/ distToTarget ;                                    endX = startX - (int)Math.round(ratio * dx);                                    endY = startY - (int)Math.round(ratio * dy);                  }                           if(startX >=400){                  dx = 400 - startX;dy = 250 - startY;                                                      double distToTarget2 = ((dx * dx + dy * dy)*2);                  double ratio2 = 5/ distToTarget2 ;                                    endX = startX + (int)Math.round(ratio2 * dx);                                    endY = startY + (int)Math.round(ratio2 * dy);                  }`

what am i missing ?
DrZoidberg

JGO Coder

Medals: 21

 « Reply #1 - Posted 2012-07-21 18:50:45 »

I assume 400,250 is the center of the screen.
Why  do you treat points right of the center differently?

I think you should treat all points equally.
Just do this
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18 `                  int startX = i;                  int startY = a;                  int dx = 0;                  int dy = 0;                  int endX=0;                  int endY=0;                                    dx = 400 - startX;dy = 250 - startY;                                                      double distToTarget = Math.sqrt(dx * dx + dy * dy);                  double ratio = 5/ distToTarget ;                                    endX = startX - (int)Math.round(ratio * dx);                                    endY = startY - (int)Math.round(ratio * dy);                  `
markmistry

Junior Devvie

 « Reply #2 - Posted 2012-07-21 19:08:35 »

Thanks, but that still just produces half an image on the left hand side going out, theres no right hand side pixels at all

update.. if you change the sign at the bottom 2 statements to positive the right hand side pixels cave in to the centre of the screen and the pixels on the right hand side dont show at all

DrZoidberg

JGO Coder

Medals: 21

 « Reply #3 - Posted 2012-07-21 19:50:10 »

Try this program

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64 `import javax.swing.*;import java.awt.*;import java.util.*;public class Pixels extends JPanel {    static Random rand = new Random();    Pixel[] pixels;    final static int width = 800, height = 500;        static class Pixel {        double x,y;        Color color;                Pixel(double x, double y, Color color) {            this.x = x;            this.y = y;            this.color = color;        }                void paint(Graphics g) {            g.setColor(color);            g.fillRect((int)x,(int)y,2,2);        }    }        public Pixels() {        pixels = new Pixel[1000];        for(int i = 0; i < pixels.length; i++) pixels[i] = new Pixel(rand.nextInt(width),                                                                     rand.nextInt(height),                                                                     new Color(rand.nextInt(0xFFFFFF)));    }    public static void main(String[] args) throws Exception {        JFrame frame = new JFrame();        Pixels panel = new Pixels();        frame.add(panel);        panel.setPreferredSize(new Dimension(width, height));        frame.pack();        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        frame.setVisible(true);                for(int i = 0; i<1000;i++) {            for(Pixel p: panel.pixels) {                double startX = p.x;                double startY = p.y;                double dx = width/2-startX;                double dy = height/2-startY;                double distToTarget = Math.sqrt(dx*dx+dy*dy);                double ratio = 0.5 / distToTarget;                double endX = startX - ratio*dx;                double endY = startY - ratio*dy;                p.x = endX;                p.y = endY;            }            Thread.sleep(10);            frame.repaint();        }    }        @Override    public void paint(Graphics g) {        g.setColor(Color.WHITE);        g.fillRect(0,0,width,height);        for(Pixel p: pixels) p.paint(g);    }`
markmistry

Junior Devvie

 « Reply #4 - Posted 2012-07-21 19:56:48 »

YES exactly like that , now to sift through your program to pick out the maths bit, thank you !!

update..

typical..it wasnt the maths after all, i was that thing when you get variable interference from somewhere else in the program, i was zeroing out a 2d array just before my loop started i put it after the array had found a pixels and hey presto it worked, btw thats some weird for loop you use, that form scares me because i dont understand it..haha

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16 `   public void imageupdate(){            bimage.createGraphics();            int rgb;                   for (int i=0; i<800; i++) {            for(int a=0;a<500;a++){                  //test2[i][a]=0;    /// /<-----this was the culprit               if(starsimage[i][a]==1){                  test2[i][a]=0;                  int startX = i;   //// <-no wonder it didnt work                  int startY = a;                                                                              int dx = 400 - startX;                     int dy = 250 - startY;`

See folks thats what happens if a newbie takes 2 years out of programming
Abuse

JGO Ninja

Medals: 70

falling into the abyss of reality

 « Reply #5 - Posted 2012-07-22 21:48:27 »

foreach

The important part is this:

Quote
When you see the colon ":" read it as “in”.

Applying this to the example:

 1 `for(Pixel p: panel.pixels)`

Quote
for each(Pixel p in panel.pixels)

I personally hated it at first, but now I've come to like it.
My only gripe with it is the occasional problem it causes when bodging new functionality into old code.
You'll sometimes find you need access to the Iterator or the iteration index and have to re-factor the loop construct - which can be a bit of a faff.
markmistry

Junior Devvie

 « Reply #6 - Posted 2012-07-23 11:08:49 »

I sort of understand it, if an iterator is not required, use that form because its faster, but that would mean changing pages of code  , may be i will experiment next year with it , as im too wrapped up in the  understandings of adding user defined classes into linkedlists and manipulating the contents..so much to learn !!
Pages: [1]
 ignore  |  Print

 hadezbladez (275 views) 2018-11-16 13:46:03 hadezbladez (153 views) 2018-11-16 13:41:33 hadezbladez (285 views) 2018-11-16 13:35:35 hadezbladez (67 views) 2018-11-16 13:32:03 EgonOlsen (2122 views) 2018-06-10 19:43:48 EgonOlsen (2146 views) 2018-06-10 19:43:44 EgonOlsen (1359 views) 2018-06-10 19:43:20 DesertCoockie (1954 views) 2018-05-13 18:23:11 nelsongames (1597 views) 2018-04-24 18:15:36 nelsongames (2244 views) 2018-04-24 18:14:32
 LiquidNitrogen 15x KaiHH 15x Spasi 13x orange451 13x bullen 9x 65K 9x Zemlaynin 8x NuclearPixels 7x polinc 7x VaTTeRGeR 5x hadezbladez 3x ral0r2 3x CommanderKeith 2x SHC 2x berzas 1x Riven 1x
 Deployment and Packagingby mudlee2018-08-22 18:09:50Java Gaming Resourcesby gouessej2018-08-22 08:19:41Deployment and Packagingby gouessej2018-08-22 08:04:08Deployment and Packagingby gouessej2018-08-22 08:03:45Deployment and Packagingby philfrei2018-08-20 02:33:38Deployment and Packagingby philfrei2018-08-20 02:29:55Deployment and Packagingby philfrei2018-08-19 23:56:20Deployment and Packagingby philfrei2018-08-19 23:54:46
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org