Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (120)
games submitted by our members
Games in WIP (577)
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 931 times)
0 Members and 1 Guest are viewing this topic.
Offline markmistry

Junior Duke





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



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 ?
Offline DrZoidberg

Senior Duke


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);
                 
Offline markmistry

Junior Duke





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

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline DrZoidberg

Senior Duke


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);
    }
Offline markmistry

Junior Duke





« 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
Offline Abuse

JGO Knight


Medals: 13


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
Offline markmistry

Junior Duke





« 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  Shocked, 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.

Longarmx (52 views)
2014-10-17 03:59:02

Norakomi (42 views)
2014-10-16 15:22:06

Norakomi (32 views)
2014-10-16 15:20:20

lcass (37 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (66 views)
2014-10-14 00:35:47

TehJavaDev (59 views)
2014-10-14 00:32:37

BurntPizza (73 views)
2014-10-11 23:24:42

BurntPizza (45 views)
2014-10-11 23:10:45

BurntPizza (85 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!