Hi, I dont get what Iv done wrong

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2009
Posts: 3
Reputation: Byrne86 is an unknown quantity at this point 
Solved Threads: 0
Byrne86 Byrne86 is offline Offline
Newbie Poster

Hi, I dont get what Iv done wrong

 
0
  #1
18 Days Ago
Hi, Iv had to convert a program from Java to C#, I thought I had done it, but it doesnt work, and Im at a loose end because I cant figure out why it isnt working. Anyway, what it is is a Mandelbrot Set that Im trying to display in a picturebox, I am drawing the fractal to a bitmap then getting the picturebox to display the bitmap. If anyone can explain what Im doing wrong that would be great.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Drawing.Drawing2D;
  10. using System.Design;
  11.  
  12. namespace Assignment_Attempt_3
  13. {
  14. public partial class Form1 : Form
  15. {
  16.  
  17.  
  18. public Form1()
  19. {
  20. InitializeComponent();
  21. Image bp = new Bitmap(this.pictureBox1.Height,this.pictureBox1.Width);
  22. Graphics g1 = Graphics.FromImage(bp);
  23. pictureBox1.Image = bp;
  24.  
  25. }
  26. public struct HSBColor
  27. {
  28. float h;
  29. float s;
  30. float b;
  31. int a;
  32.  
  33. public HSBColor(float h, float s, float b)
  34. {
  35. this.a = 0xff;
  36. this.h = Math.Min(Math.Max(h, 0), 255);
  37. this.s = Math.Min(Math.Max(s, 0), 255);
  38. this.b = Math.Min(Math.Max(b, 0), 255);
  39. }
  40.  
  41. public HSBColor(int a, float h, float s, float b)
  42. {
  43. this.a = a;
  44. this.h = Math.Min(Math.Max(h, 0), 255);
  45. this.s = Math.Min(Math.Max(s, 0), 255);
  46. this.b = Math.Min(Math.Max(b, 0), 255);
  47. }
  48.  
  49. public float H
  50. {
  51. get { return h; }
  52. }
  53.  
  54. public float S
  55. {
  56. get { return s; }
  57. }
  58.  
  59. public float B
  60. {
  61. get { return b; }
  62. }
  63.  
  64. public int A
  65. {
  66. get { return a; }
  67. }
  68.  
  69. public Color Color
  70. {
  71. get
  72. {
  73. return FromHSB(this);
  74. }
  75. }
  76.  
  77. public static Color FromHSB(HSBColor hsbColor)
  78. {
  79. float r = hsbColor.b;
  80. float g = hsbColor.b;
  81. float b = hsbColor.b;
  82. if (hsbColor.s != 0)
  83. {
  84. float max = hsbColor.b;
  85. float dif = hsbColor.b * hsbColor.s / 255f;
  86. float min = hsbColor.b - dif;
  87.  
  88. float h = hsbColor.h * 360f / 255f;
  89.  
  90. if (h < 60f)
  91. {
  92. r = max;
  93. g = h * dif / 60f + min;
  94. b = min;
  95. }
  96. else if (h < 120f)
  97. {
  98. r = -(h - 120f) * dif / 60f + min;
  99. g = max;
  100. b = min;
  101. }
  102. else if (h < 180f)
  103. {
  104. r = min;
  105. g = max;
  106. b = (h - 120f) * dif / 60f + min;
  107. }
  108. else if (h < 240f)
  109. {
  110. r = min;
  111. g = -(h - 240f) * dif / 60f + min;
  112. b = max;
  113. }
  114. else if (h < 300f)
  115. {
  116. r = (h - 240f) * dif / 60f + min;
  117. g = min;
  118. b = max;
  119. }
  120. else if (h <= 360f)
  121. {
  122. r = max;
  123. g = min;
  124. b = -(h - 360f) * dif / 60 + min;
  125. }
  126. else
  127. {
  128. r = 0;
  129. g = 0;
  130. b = 0;
  131. }
  132. }
  133.  
  134. return Color.FromArgb
  135. (
  136. hsbColor.a,
  137. (int)Math.Round(Math.Min(Math.Max(r, 0), 255)),
  138. (int)Math.Round(Math.Min(Math.Max(g, 0), 255)),
  139. (int)Math.Round(Math.Min(Math.Max(b, 0), 255))
  140. );
  141. }
  142.  
  143. }
  144. private const int MAX = 256; // max iterations
  145. private const double SX = -2.025; // start value real
  146. private const double SY = -1.125; // start value imaginary
  147. private const double EX = 0.6; // end value real
  148. private const double EY = 1.125; // end value imaginary
  149. private static int x1, y1, xs, ys, xe, ye;
  150. private static double xstart, ystart, xende, yende, xzoom, yzoom;
  151. private static bool action, rectangle, finished;
  152. private static float xy;
  153. private Image bp;
  154. private Graphics g1;
  155. //private HSB HSBcol=new HSB();
  156.  
  157. public void init() // all instances will be prepared
  158. {
  159. //HSBcol = new HSB();
  160. this.Size = new Size(640, 480);
  161. finished = false;
  162. x1 = this.Width;
  163. y1 = this.Height;
  164. xy = (float)x1 / (float)y1;
  165. //?JAVA? picture = createImage(x1, y1);
  166. //?JAVA? g1 = picture.getGraphics();
  167. finished = true;
  168. mandelbrot();
  169. }
  170.  
  171. public void destroy() // delete all instances
  172. {
  173. if (finished)
  174. {
  175. bp = null;
  176. g1 = null;
  177. GC.Collect(); // garbage collection
  178. }
  179. }
  180.  
  181. public void start()
  182. {
  183. action = false;
  184. rectangle = false;
  185. initvalues();
  186. xzoom = (xende - xstart) / (double)x1;
  187. yzoom = (yende - ystart) / (double)y1;
  188. mandelbrot();
  189. }
  190.  
  191. public void stop()
  192. {
  193. }
  194.  
  195. private void mandelbrot() // calculate all points
  196. {
  197. int x, y;
  198. float h, b, alt = 0.0f;
  199.  
  200. action = false;
  201. for (x = 0; x < x1; x += 2)
  202. for (y = 0; y < y1; y++)
  203. {
  204. h = pointcolour(xstart + xzoom * (double)x, ystart + yzoom * (double)y); // color value
  205. if (h != alt)
  206. {
  207. b = 1.0f - h * h; // brightnes
  208.  
  209. Color color = HSBColor.FromHSB(new HSBColor(h * 255, 0.8f * 255, b * 255)); // VERY IMPORTANT
  210. //djm
  211. alt = h;
  212. Pen pen = new Pen(color);
  213. g1.DrawLine(pen, x, y, x + 1, y);
  214. }
  215. }
  216. action = true;
  217. }
  218.  
  219. private float pointcolour(double xwert, double ywert) // color value from 0.0 to 1.0 by iterations
  220. {
  221. double r = 0.0, i = 0.0, m = 0.0;
  222. int j = 0;
  223.  
  224. while ((j < MAX) && (m < 4.0))
  225. {
  226. j++;
  227. m = r * r - i * i;
  228. i = 2.0 * r * i + ywert;
  229. r = m + xwert;
  230. }
  231. return (float)j / (float)MAX;
  232. }
  233.  
  234. private void initvalues() // reset start values
  235. {
  236. xstart = SX;
  237. ystart = SY;
  238. xende = EX;
  239. yende = EY;
  240. if ((float)((xende - xstart) / (yende - ystart)) != xy)
  241. xstart = xende - (yende - ystart) * (double)xy;
  242. }
  243.  
  244. private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
  245. {
  246. //e.consume();
  247. if (action)
  248. {
  249. xs = e.X;
  250. ys = e.Y;
  251. }
  252. }
  253.  
  254. private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
  255. {
  256. int z, w;
  257.  
  258. //e.consume();
  259. if (action)
  260. {
  261. xe = e.X;
  262. ye = e.Y;
  263. if (xs > xe)
  264. {
  265. z = xs;
  266. xs = xe;
  267. xe = z;
  268. }
  269. if (ys > ye)
  270. {
  271. z = ys;
  272. ys = ye;
  273. ye = z;
  274. }
  275. w = (xe - xs);
  276. z = (ye - ys);
  277. if ((w < 2) && (z < 2)) initvalues();
  278. else
  279. {
  280. if (((float)w > (float)z * xy)) ye = (int)((float)ys + (float)w / xy);
  281. else xe = (int)((float)xs + (float)z * xy);
  282. xende = xstart + xzoom * (double)xe;
  283. yende = ystart + yzoom * (double)ye;
  284. xstart += xzoom * (double)xs;
  285. ystart += yzoom * (double)ys;
  286. }
  287. xzoom = (xende - xstart) / (double)x1;
  288. yzoom = (yende - ystart) / (double)y1;
  289. mandelbrot();
  290. rectangle = false;
  291. Refresh();
  292. }
  293.  
  294. }
  295.  
  296. private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
  297. {
  298. //e.consume();
  299. if (action)
  300. {
  301. xe = e.X;
  302. ye = e.Y;
  303. rectangle = true;
  304. Refresh();
  305. }
  306. }
  307.  
  308.  
  309. }
  310. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,908
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 273
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso
 
0
  #2
18 Days Ago
Hi Byrne86 welcome at DANIWEB.
On first inspection of your code I can tell you (unless I overlooked something) you are never calling your Init and Start methods.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 3
Reputation: Byrne86 is an unknown quantity at this point 
Solved Threads: 0
Byrne86 Byrne86 is offline Offline
Newbie Poster
 
0
  #3
18 Days Ago
Sorry, I forgot to mention some other things, Iv tried calling the start() and init() methods using a button. If there is anything else you can spot I would be very grateful.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,908
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 273
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso
 
0
  #4
18 Days Ago
You are defining some variables twice on line 21 and 22
and on line 153 and 154. I should remove the types in the constructor on line 21 and 22.
Instead of Image bp... just bp... etc.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,908
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 273
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso
 
0
  #5
18 Days Ago
On line 160 the code is: this.Size = new Size(640, 480);
this refers here to the form, I think you want to do: this.pictureBox1.Size..
Set the borderstyle property of your pictureBox1 to FixedSingle, so you can see where it is on the form.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,908
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 273
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso
 
1
  #6
17 Days Ago
This is what I got so far, with some minor changes to your code :
(see attachement)
  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. // ddanbe Start button click code
  4. init();
  5. start();
  6. Refresh(); //----->added ddanbe
  7. }
and
  1. public void init() // all instances will be prepared
  2. {
  3. //HSBcol = new HSB();
  4. //this.Size = new Size(640, 480);
  5. // ---->Size done in properties of pictureBox1
  6. // also add FixedSingle as BorderStyle
  7. // ---->Set size of form in designer accordingly
  8. finished = false;
  9. x1 = this.pictureBox1.Width; //---->added pictureBox1
  10. y1 = this.pictureBox1.Height; //---->added pictureBox1
  11. xy = (float)x1 / (float)y1;
  12. //?JAVA? picture = createImage(x1, y1);
  13. //?JAVA? g1 = picture.getGraphics();
  14. finished = true;
  15. //mandelbrot(); ---> is called in Start method
  16. }
Last edited by ddanbe; 17 Days Ago at 2:04 pm.
Attached Thumbnails
MandelTry.jpg  
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Reply


Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC