Win a copy of Secure Financial Transactions with Ansible, Terraform, and OpenSCAP this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Mandelbrot

 
Ranch Hand
Posts: 197
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I have slightly modified this code found in rosetta code
it does not work
Thank you for your help
 
Sheriff
Posts: 4968
318
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're going to need to be more specific than "it does not work".
  • What are you expecting it to do?
  • What does it actually do?
  • What steps to resolve it have you tried out?
  •  
    ronald deux
    Ranch Hand
    Posts: 197
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    what interests me is to make a mandelbrot with several colors
    I think line 31 is wrong
     
    Marshal
    Posts: 26493
    81
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Okay; that answers the first of Tim's three bullet points. Would you like to try answering the second point?
     
    Master Rancher
    Posts: 3831
    50
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Those bullet points are really useful for (a) organizing your own thinking, and then (b) convincing others to help you.  But jumping past that... line 31 is the one that throws the exception.  That doesn't necessarily mean it's the problem.  Take a close look at line 19.
     
    ronald deux
    Ranch Hand
    Posts: 197
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    ok i found the error
    I thought I had made a mistake compared to another mandelbrot
     
    Marshal
    Posts: 72478
    315
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Well done finding the error; what was it?
     
    ronald deux
    Ranch Hand
    Posts: 197
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    x ++ instead of y ++
    line 19
     
    ronald deux
    Ranch Hand
    Posts: 197
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    I want to add this piece of code
    I replace setcolor with I.setRGB at each iteration
    I think
    and then ???
    have any suggestions?
     
    Saloon Keeper
    Posts: 4358
    163
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    An alternative is to have a Map<Integer, Color>, so that you can replace all the ifs with just the line:

    Of course you have to make that Map in the first place. To save on work, you might use a formula to derive the color from the value of iter.
    For instance (using the red color)
     
    Saloon Keeper
    Posts: 6896
    163
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    A long time ago I wrote a Mandelbrot applet where I special-cased the exponents 2 and 3 in order to reduce the number of multiplications, by using these formulas:

    This would reduce the number of multiplications from 4 to 2 for exponent 2, and from 8 to 6 for exponent 3, if memory serves. That brings back memories :-) I have no idea if that would make much of a difference these days.
     
    Piet Souris
    Saloon Keeper
    Posts: 4358
    163
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Them good old days!

    I remember that we got PCs at the office, was it early 90's or late 80's? The first thing we put on these things were Mandelbrots, with QBasic. But the processors were 486SX, so that took quite a while.

    @OP
    You start your calculations with an (x, y) for which x < -2 (actually -400 / 150). Since points with |p| > 2 will have an iter of 0 you might as well not calculate them. So, if your zoom = 150, you can limit your panel to a width and height of 600.

    Also nice is to map an iter-value of 1* to a fully transparent color. If you save your BufferedImage, you can paint it later in any text, without worrying about the ugly outer parts.

    * since you start with evaluating (0, 0) when iter = 0




























     
    ronald deux
    Ranch Hand
    Posts: 197
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I do not know how to do
    I don't really understand where you are coming from
     
    Piet Souris
    Saloon Keeper
    Posts: 4358
    163
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Okay, never mind then.
     
    ronald deux
    Ranch Hand
    Posts: 197
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    I made this piece of code,
    I try to make a mandelbrot with several colors
    Thank you for your help
     
    Piet Souris
    Saloon Keeper
    Posts: 4358
    163
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    hi  Ronald,

    that is the right idea, yes. I just made a demo (to rehearse long forgotten methods), that is a bit more luxuous than what you showed in your opening post.
    There are three things to set: a Point2D that indicates the topleft point of the rectangle for which the Mandelbrot must be calculated, likewise a bottomright point, and lastly the width of the panel. That's all needed to get you a nice Mandelbrot.

    I opted for a complete random colormap, but I outcommented a more structured one, for you to try.

    I used a javaFX Point2D to indicate a point, because such a Point has the nice method 'distance', and that limits many of those horrible looking squares.
    To zoom in on a part, just give the points topLeft and bottomRight a much smaller rectangle.

    Experiment, and have fun!
     
    ronald deux
    Ranch Hand
    Posts: 197
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    thanks for the code, i thought i was abandoned
    I made some corrections for your code
    instead of javafx.geometry.Point2D
    I put java.awt.geom.Point2D

    also Point 2D.Double instead of Point2D,

    and I block at p = square (p) .add (c)

    I think I will have to revise my java lessons
    and that I learn more

    thank you
     
    Piet Souris
    Saloon Keeper
    Posts: 4358
    163
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    A short explanation about this mysterious 'p = square(p).add(c)':

    Suppose you have a function f(z) = z^2 + c, where c is a constant. In this, z and c are complex numbers.

    What we do in a Mandelbrot program is to pick a point c, and then evaluate the series c, f(c), f(f(c)), et cetera. As soon as one of these points has a distance to the origin of more than 4, we count how many iterations we needed for that, and color the point c according to that number. However, there are points c that you can iterate forever, without getting more than 4 from the origin. The Mandelbrot collection consists of all the points c with this property. Now, we cannot do an infinite number of iterations, so we have a number MAX_ITER, and we say that when we have done MAX_ITER iterations, and the point is STILL within the distance of 4, then we assume that point c to belong to the Mandelbrot set.

    So, what we do is: for x = 0 to panelwidth and y = 0 to panelheight, determine which point c belongs to these x and y, and then we iterate:

    c, c^2 + c, ((c^2 + c)^2 + c, et cetera, and see when a value has a distance of more than 4 from the origin.

    So, if I have a point p, I iterate p = p * p + c, or p = square(p).add(c).

    Last remark: multiplying a point with itself?? Yes, with complex numers that is possible.
     
    ronald deux
    Ranch Hand
    Posts: 197
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks for the explanation
    I block on p = square (p) .add (c); because I cannot correct add which is underlined in red
    it says add cast to square (p)
     
    Piet Souris
    Saloon Keeper
    Posts: 4358
    163
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Well, you are using Java Point2D.Double, and I am using JavaFX Point2D, and that has the method 'add'.

    By the way: there is in java(and FX) a class AffineTransform (in FX called Affine), that can map one set of coordinates to another set of coordinates. What we do in a Mandelbrot: we have pixelcoordinates, and we transform them to cartesian coordinates. Suppose we have such a transform called 'at'. My code now looks like:

    And as you see: no horrible squares anymore!  
     
    ronald deux
    Ranch Hand
    Posts: 197
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    thank you
     
    And tomorrow is the circus! We can go to the circus! I love the circus! We can take this tiny ad:
    SKIP - a book about connecting industrious people with elderly land owners
    https://coderanch.com/t/skip-book
    reply
      Bookmark Topic Watch Topic
    • New Topic