Link to home
Start Free TrialLog in
Avatar of pouli
pouli

asked on

Curves and x, y points

Hello everybody,

What I want is straightforward.
I need all the X, Y pairs of points of a quadratic curve.

For example I know the x1, y1 & x2, y2 and finally ctrX1 & ctrY1 points
to make the quadratic curve.

But I need to know the x,y points that this line is made of so I wil be able to
make an animation of an image up to this path. I itend not to draw the actual
line.

I found the PathIterator in the API but this does not really solve my problem.
From my understanding it reconstructs a complex shape with the aid of simpler ones.

It doesn't give any clues about the x,y points.
If I am wrong please tell me.

I found also a piece of code that I change it a bit to fit my needs. This will be
the worst solution though.
I cannot believe that for that simple problem I need to do the following. There must
be a simpler way.

What I found is to:
1. Make the curve
2. Take the Rectangle for that Shape with getBounds()
3. Make a smaller curve line
4. Take the rectangle for this as well
5. Subtract the areas (Not know how to do this)
6. Find the x, y points for the final area

Actually what I have just said is not the worst solution.
I thought somwthing even worst than this. :)
Anyway

I am not sure if this is possible.
What I have on hands is that foloowing code.

QuadCurve2D.Double q2d2 = new QuadCurve2D.Double( 10, 200, 95, 50, 200, 200);
r1   = q2d.getBounds2D().getBounds();

for (int x=0; x < r1.width; x++)
{
     for (int y=0; y < r1.height; y++)    
     {          
          if (ar1.intersects(r1.x+x,r1.y+y,1,1))   // this point is on the arc          
          {
               g2d.fillRect(r1.x + x, r1.y + y, 1, 1);
          }
     }
}

I do not know how to take the Area though form the previous shape.
Anybody can help me with some sample solution ???
Any comments ??
Other simpler ways ?
Do you thing I am in the right trck ?
Thank you in advance.
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

What you need to do is to derive the quadratic function from x1,y1,x2,y2 then you can derive all y points you need in the intervals. If you have a problem with this, give us the values of the above points.
Avatar of pouli
pouli

ASKER

I think there should be another way to get around without maths.

My maths are not good. But could you give me the equation, if you know it ?

I could see the source code for the Quadratic class but I think it will be easier if you know it to tell it to me :)


>If you have a problem with this, give us the values of >the above points

I have some values in the code. If this is what you mean.
What exactly is it you want to paint?
OK, I need you to give me two valid pairs of values.
Come to think of it, I think I'll need 3 pairs of points. Where are these points coming from anyway? How do you know it's a quadratic finction?
Avatar of pouli

ASKER

> What exactly is it you want to paint?
It is JLabel's images.

> OK, I need you to give me two valid pairs of values.
> Come to think of it, I think I'll need 3 pairs of  
> points. Where are these points coming from anyway? How  >do you know it's a quadratic finction?

QuadCurve2D.Double q2d2 = new QuadCurve2D.Double( 10, 200, 95, 50, 200, 200);

The result of what I want to do is the result of the previous line of code.

The x1, y1 & x2, y2 & ctrX1 & ctrX2 points will be changing according to my needss. They will simply be points on the screen. You could assume that is going to be anywhere on screen.

I do not know the quadratic function with 1 contol point. Do you ?

Anybody can think of any way find these points ?
Avatar of pouli

ASKER

Hello, again

I think that I must give the points to myself :-)
I found the answer. Unfortunately to a web site that is probably supported by M$ :-(

Anyway the writer is open minded.

The URL is :

http://www.maxcode.com/nuke/article.php?sid=232

It is for a Bezier line with 2 control points but if we give the same values to the 2 control points then we have the Bezier line with 1 control point :-)

And that's all.

Thank you all for your interest.

ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pouli

ASKER

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class BezierLine extends JFrame
{

     Graphics2D g2d;
     
     
     double x1 = 100;
     double y1 = 100;
         
     double x2 = 300;
     double y2 = 300;
     
     double xa = 300;
     double ya = 50;
     
     double xb = 300;
     double yb = 50;
     
     public BezierLine()
     {
          setSize( 500, 500 );
          addMouseListener( new mouseListener() );
          show();
     }
     
     
     public void update( Graphics g )
     {
          paint( g );
     }
     
     public void paint( Graphics g )
     {
          g2d = (Graphics2D) g;
          double v2, v1;
          double x, y;
         
          int temp = 0;
          for( v1 = 0; v1 < 1; v1 = v1 + 0.01 )
          {
               v2 = 1.0 - v1;
               x = ( x1 * Math.pow(v2, 3.0) + (3.0 * xa * v1 * v2 * v2) + (3.0 * xb * v1 * v1 * v2) + (x2 * Math.pow(v1, 3.0)));
               y = ( y1 * Math.pow(v2, 3.0) + (3.0 * ya * v1 * v2 * v2) + (3.0 * yb * v1 * v1 * v2) + (y2 * Math.pow(v1, 3.0)));
               g2d.fillRect((int)x, (int)y, 1, 1);
               temp++;
          }          
          System.out.println(temp);
         
          g2d.fillRect( (int)xa, (int)ya, 3, 3 );
          g2d.fillRect( (int)xb, (int)yb, 3, 3 );
     }
     
     public static void main( String args[] )
     {
          new BezierLine();
     }
     
     private class mouseListener extends MouseAdapter
     {
          public void mouseClicked(MouseEvent e)
          {
               System.out.println("X is " + e.getX() + ", Y is " + e.getY() );
          }
     }
     
     
}
Avatar of pouli

ASKER

This is a sample program that I made in order to check how it works.

My itention is not to draw the lines but accumulate the 100 points into an array and then pass them to a Thread that will animate the image along that path
:-)

I always support though the people who support me.
Avatar of pouli

ASKER

Objects You told me once that the paint method is for the old version of Java, and I should not use it now.

COuld you give me the better version (1.4) from the previous program.

Or was it a missunderstanding ??
From reading the Javadoc the Quad2DCurve.getPathIterator appears to return the points you are after. I'd try it.

Regards using paint, it is still used for heavyweight components. It's just lightweights that you should use paintComponent for.

thanks for the points :)

http://www.objects.com.au/staff/mick
Brainbench MVP for Java 1
http://www.brainbench.com
Avatar of pouli

ASKER

>From PathIterator javadoc:
>
>"This interface allows these objects to retrieve the path >of their boundary a segment at a time by using 1st >through 3rd order Bézier curves, which are lines and >quadratic or cubic Bézier splines. "

Objects I tried the PathIteratorand what I understood was that it breaks a complex shape into other smaller components using 1st & 3rd order Bezier curves. It gives the initial and final points but not the intermediate.

If I am wrong please tell me I would like to know how this can be done with Java api's.