Java-Gaming.org
 Featured games (81) games approved by the League of Dukes Games in Showcase (497) Games in Android Showcase (114) games submitted by our members Games in WIP (563) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
 Home Help Search Login Register
Pages: [1]
 ignore  |  Print
 help ! stuck with maths problem i think..[SOLVED]  (Read 923 times) 0 Members and 1 Guest are viewing this topic.
markmistry

Junior Member

 « 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

Senior Member

Medals: 15

 « 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 Member

 « 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

 Games published by our own members! Check 'em out!
DrZoidberg

Senior Member

Medals: 15

 « 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 Member

 « 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 Knight

Medals: 12

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)`

...should be read as...

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.

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
markmistry

Junior Member

 « 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

You cannot reply to this message, because it is very, very old.

 Add your game by posting it in the WIP section, or publish it in Showcase. The first screenshot will be displayed as a thumbnail.
 BurntPizza (22 views) 2014-09-19 03:14:18 Dwinin (35 views) 2014-09-12 09:08:26 Norakomi (62 views) 2014-09-10 13:57:51 TehJavaDev (88 views) 2014-09-10 06:39:09 Tekkerue (44 views) 2014-09-09 02:24:56 mitcheeb (65 views) 2014-09-08 06:06:29 BurntPizza (47 views) 2014-09-07 01:13:42 Longarmx (35 views) 2014-09-07 01:12:14 Longarmx (40 views) 2014-09-07 01:11:22 Longarmx (36 views) 2014-09-07 01:10:19
 BurntPizza 37x Riven 18x Rayvolution 18x princec 17x ags1 16x basil_ 16x KevinWorkman 15x LiquidNitrogen 12x kevglass 12x theagentd 11x nsigma 11x HeroesGraveDev 9x deathpat 9x The Lion King 7x TehJavaDev 6x Gibbo3771 6x
 List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27Resources for WIP games2014-08-01 16:20:17Resources for WIP games2014-08-01 16:19:50List of Learning Resources2014-07-31 16:29:50List of Learning Resources2014-07-31 16:26:06List of Learning Resources2014-07-31 11:54:12HotSpot Optionsby dleskov2014-07-08 01:59:08
 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