   problem with Perlin Noise
  Hi,Im trying to implement perlin noise, but im getting errors int he rendering. Could anyone take a look at the code to see where im going wrong?Everything ive found online deals with 3d perlin noise, and im trying to get 2d perlin noise to work, so this is probaly where im going wrong.The code is in XNA. Ive also added the project if anyone would find that easier to play with.Thanksusing System;using System.Collections.Generic;using System.Linq;using Microsoft.Xna.Framework;using Microsoft.Xna.Framework.Audio;using Microsoft.Xna.Framework.Content;using Microsoft.Xna.Framework.GamerServices;using Microsoft.Xna.Framework.Graphics;using Microsoft.Xna.Framework.Input;using Microsoft.Xna.Framework.Media;using Microsoft.Xna.Framework.Net;using Microsoft.Xna.Framework.Storage;namespace BookCode{    public class Game1 : Microsoft.Xna.Framework.Game    {   GraphicsDeviceManager graphics;   GraphicsDevice device;   SpriteBatch spriteBatch;   Texture2D tex;   byte[] random = new byte[256];   Vector2[] gradients = new Vector2[4];   public Game1()   {  graphics = new GraphicsDeviceManager(this);  Content.RootDirectory = "Content";   }   protected override void Initialize()   {  base.Initialize();   }   protected override void LoadContent()   {  device = graphics.GraphicsDevice;  spriteBatch = new SpriteBatch(GraphicsDevice);  int winX = this.Window.ClientBounds.Width;  int winY = this.Window.ClientBounds.Height;  tex = new Texture2D(device, winX, winY);  Color[] rcol = new Color[winX * winY];  gradients[0] = new Vector2(1, 1);  gradients[1] = new Vector2(-1, 1);  gradients[2] = new Vector2(1, -1);  gradients[3] = new Vector2(-1, -1);  Random r = new Random();  for (int i = 0; i < 256; i++)  { random = (byte)r.Next(0, 256);  }    float num = 0.02f;  Vector2 P = new Vector2();  for (int i = 0; i < winY; i++)  { for (int j = 0; j < winX; j++) {float result = noise2d(P);//float f1 = noise2d(P);//float f2 = noise2d(2 * P);//float f3 = noise2d(4 * P);//float f4 = noise2d(8 * P);//float f5 = noise2d(16 * P);//float result = 0.5f * f1 +// 0.25f * f2 +// 0.125f * f3 +//  0.06f * f4 +//   0.003f * f5; result += 1.0f;result *= 0.5f;byte b = (byte)(255.0f * result);//fill temp texture array rcol = <span class="cpp-keyword">new</span> Color(b, b, b);P.X += num; } P.X = <span class="cpp-number">0</span>.0f; P.Y += num;  }  <span class="cpp-comment">//fill actual texture</span>  tex.SetData&lt;Color&gt;(rcol);   }   <span class="cpp-keyword">private</span> <span class="cpp-keyword">float</span> noise2d(Vector2 Pos)   {  Vector2 T = <span class="cpp-keyword">new</span> Vector2(Pos.X - (<span class="cpp-keyword">int</span>)Pos.X, Pos.Y - (<span class="cpp-keyword">int</span>)Pos.Y);  <span class="cpp-keyword">float</span> fx = fade(T.X);  <span class="cpp-keyword">float</span> fy = fade(T.Y);  <span class="cpp-keyword">float</span> AA = perm(Pos.X) + Pos.Y;  <span class="cpp-keyword">float</span> AB = perm(AA + <span class="cpp-number">1</span>);  <span class="cpp-keyword">float</span> BA = perm(Pos.X + <span class="cpp-number">1</span>) + Pos.Y;  <span class="cpp-keyword">float</span> BB = perm(BA + <span class="cpp-number">1</span>);  <span class="cpp-keyword">float</span> u = MathHelper.Lerp(grad(perm(AA), T),grad(perm(BA), T + <span class="cpp-keyword">new</span> Vector2(-<span class="cpp-number">1</span>, <span class="cpp-number">0</span>)),fx);  <span class="cpp-keyword">float</span> v = MathHelper.Lerp(grad(perm(AB), T + <span class="cpp-keyword">new</span> Vector2(<span class="cpp-number">0</span>, -<span class="cpp-number">1</span>)),grad(perm(BB), T + <span class="cpp-keyword">new</span> Vector2(-<span class="cpp-number">1</span>, -<span class="cpp-number">1</span>)),fx);  <span class="cpp-keyword">float</span> final = MathHelper.Lerp(u, v, fy);  <span class="cpp-keyword">return</span> final;   }   <span class="cpp-keyword">float</span> fade(<span class="cpp-keyword">float</span> t)   {  <span class="cpp-keyword">return</span> t * t * t * (t * (t * <span class="cpp-number">6</span> - <span class="cpp-number">15</span>) + <span class="cpp-number">10</span>);   }   <span class="cpp-keyword">private</span> <span class="cpp-keyword">float</span> grad(byte x, Vector2 pos)   {  <span class="cpp-keyword">if</span> (x &gt;= <span class="cpp-number">4</span>) x = (byte)(x % <span class="cpp-number">4</span>);  Vector2 g = gradients[x];  <span class="cpp-keyword">return</span> Vector2.Dot(g, pos);   }   <span class="cpp-keyword">private</span> byte perm(<span class="cpp-keyword">float</span> v)   {  <span class="cpp-keyword">int</span> v2 = (<span class="cpp-keyword">int</span>)Math.Floor(v);  byte v3;  <span class="cpp-keyword">if</span> (v2 &gt; <span class="cpp-number">255</span>) v3 = (byte)(v2 % <span class="cpp-number">255</span>);  <span class="cpp-keyword">else</span> v3 = (byte)v2;  <span class="cpp-keyword">return</span> random[v3];   }   <span class="cpp-keyword">protected</span> override <span class="cpp-keyword">void</span> UnloadContent()   {   }   <span class="cpp-keyword">protected</span> override <span class="cpp-keyword">void</span> Update(GameTime gameTime)   {  <span class="cpp-keyword">if</span> (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) <span class="cpp-keyword">this</span>.Exit();  base.Update(gameTime);   }   <span class="cpp-keyword">protected</span> override <span class="cpp-keyword">void</span> Draw(GameTime gameTime)   {  device.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.CornflowerBlue, <span class="cpp-number">1</span>, <span class="cpp-number">0</span>);  spriteBatch.Begin();  spriteBatch.Draw(tex, <span class="cpp-keyword">new</span> Vector2(<span class="cpp-number">0</span>, <span class="cpp-number">0</span>), Color.White);  spriteBatch.End();  base.Draw(gameTime);   }    }}</pre></div><!–ENDSCRIPT–><a href="http://www.3dpeanut.co.uk/Crap/noise.zip">Source Code</a> 
  From the image, I would say that your gradients are being flipped in the vertical direction, but I don't immediately seea problem with your code.   ;  Ive noticed that the problem occurs, each time the Y position hits its next integeral value, such as 1, 2, 3 etc etc. Any ideas?   ;Quote:Original post by wforlIve noticed that the problem occurs, each time the Y position hits its next integeral value, such as 1, 2, 3 etc etc. Any ideas?I think the problem is that you are using the input position (Pos) and the fractional portion of it (T). Instead you should be using the integer portion and the fractional portion, as Perlin does here.   ;  hi,I get the integer portion in the perm function. So i believe im following it correctly.Well, im going to try implementing the 3d noise, as that way i can follow it step by step without making my own bits up. And see how that goes   ;  here is my implementation of the perlin2 function (I quickly pulled it out of a larger file, so let me know if it's missing something that you need). It works. I trust you can have a look and compare it to yours to find the problem. Since you're writing managed code, ignore the f2iRoundFast function and just settle for a standard int cast instead. Good luck!// the functionfloat NoiseWrapper::perlinNoise2(float x, float y){    int ix = f2iRoundFast(x - 0.5f);    int iy = f2iRoundFast(y - 0.5f);    x -= ix; y -= iy;    float x1 = x-1.0f; float y1 = y-1.0f;    ix &= 255; iy &= 255;    float s = qCurve(x);    float t = qCurve(y);    int a = p[ix  ]+iy;    int b = p[ix+1]+iy;    return lerp( lerp( grad(p[a  ],x ,y ),   grad(p,x1,y ), s),<br>  lerp( grad(p[a+<span class="cpp-number">1</span>],x ,y1),<br>   grad(p[b+<span class="cpp-number">1</span>],x1,y1), s), t) * PERLIN2_MULT;<br>}<br><br><span class="cpp-comment">// some stuff that the function uses</span><br><span class="cpp-directive">#define</span> PERLIN2_MULT<span class="cpp-number">0</span>.507f<br><br><span class="cpp-keyword">const</span> <span class="cpp-keyword">unsigned</span> <span class="cpp-keyword">char</span> NoiseWrapper::p[<span class="cpp-number">512</span>] = {<br>    <span class="cpp-number">151</span>,<span class="cpp-number">160</span>,<span class="cpp-number">137</span>, <span class="cpp-number">91</span>, <span class="cpp-number">90</span>, <span class="cpp-number">15</span>,<span class="cpp-number">131</span>, <span class="cpp-number">13</span>,<span class="cpp-number">201</span>, <span class="cpp-number">95</span>, <span class="cpp-number">96</span>, <span class="cpp-number">53</span>,<span class="cpp-number">194</span>,<span class="cpp-number">233</span>,  <span class="cpp-number">7</span>,<span class="cpp-number">225</span>,<span class="cpp-number">140</span>, <span class="cpp-number">36</span>,<span class="cpp-number">103</span>, <span class="cpp-number">30</span>,<br><span class="cpp-number">69</span>,<span class="cpp-number">142</span>,  <span class="cpp-number">8</span>, <span class="cpp-number">99</span>, <span class="cpp-number">37</span>,<span class="cpp-number">240</span>, <span class="cpp-number">21</span>, <span class="cpp-number">10</span>, <span class="cpp-number">23</span>,<span class="cpp-number">190</span>,  <span class="cpp-number">6</span>,<span class="cpp-number">148</span>,<span class="cpp-number">247</span>,<span class="cpp-number">120</span>,<span class="cpp-number">234</span>, <span class="cpp-number">75</span>,  <span class="cpp-number">0</span>, <span class="cpp-number">26</span>,<span class="cpp-number">197</span>, <span class="cpp-number">62</span>,<br><span class="cpp-number">94</span>,<span class="cpp-number">252</span>,<span class="cpp-number">219</span>,<span class="cpp-number">203</span>,<span class="cpp-number">117</span>, <span class="cpp-number">35</span>, <span class="cpp-number">11</span>, <span class="cpp-number">32</span>, <span class="cpp-number">57</span>,<span class="cpp-number">177</span>, <span class="cpp-number">33</span>, <span class="cpp-number">88</span>,<span class="cpp-number">237</span>,<span class="cpp-number">149</span>, <span class="cpp-number">56</span>, <span class="cpp-number">87</span>,<span class="cpp-number">174</span>, <span class="cpp-number">20</span>,<span class="cpp-number">125</span>,<span class="cpp-number">136</span>,<br>    <span class="cpp-number">171</span>,<span class="cpp-number">168</span>, <span class="cpp-number">68</span>,<span class="cpp-number">175</span>, <span class="cpp-number">74</span>,<span class="cpp-number">165</span>, <span class="cpp-number">71</span>,<span class="cpp-number">134</span>,<span class="cpp-number">139</span>, <span class="cpp-number">48</span>, <span class="cpp-number">27</span>,<span class="cpp-number">166</span>, <span class="cpp-number">77</span>,<span class="cpp-number">146</span>,<span class="cpp-number">158</span>,<span class="cpp-number">231</span>, <span class="cpp-number">83</span>,<span class="cpp-number">111</span>,<span class="cpp-number">229</span>,<span class="cpp-number">122</span>,<br><span class="cpp-number">60</span>,<span class="cpp-number">211</span>,<span class="cpp-number">133</span>,<span class="cpp-number">230</span>,<span class="cpp-number">220</span>,<span class="cpp-number">105</span>, <span class="cpp-number">92</span>, <span class="cpp-number">41</span>, <span class="cpp-number">55</span>, <span class="cpp-number">46</span>,<span class="cpp-number">245</span>, <span class="cpp-number">40</span>,<span class="cpp-number">244</span>,<span class="cpp-number">102</span>,<span class="cpp-number">143</span>, <span class="cpp-number">54</span>, <span class="cpp-number">65</span>, <span class="cpp-number">25</span>, <span class="cpp-number">63</span>,<span class="cpp-number">161</span>,<br> <span class="cpp-number">1</span>,<span class="cpp-number">216</span>, <span class="cpp-number">80</span>, <span class="cpp-number">73</span>,<span class="cpp-number">209</span>, <span class="cpp-number">76</span>,<span class="cpp-number">132</span>,<span class="cpp-number">187</span>,<span class="cpp-number">208</span>, <span class="cpp-number">89</span>, <span class="cpp-number">18</span>,<span class="cpp-number">169</span>,<span class="cpp-number">200</span>,<span class="cpp-number">196</span>,<span class="cpp-number">135</span>,<span class="cpp-number">130</span>,<span class="cpp-number">116</span>,<span class="cpp-number">188</span>,<span class="cpp-number">159</span>, <span class="cpp-number">86</span>,<br>    <span class="cpp-number">164</span>,<span class="cpp-number">100</span>,<span class="cpp-number">109</span>,<span class="cpp-number">198</span>,<span class="cpp-number">173</span>,<span class="cpp-number">186</span>,  <span class="cpp-number">3</span>, <span class="cpp-number">64</span>, <span class="cpp-number">52</span>,<span class="cpp-number">217</span>,<span class="cpp-number">226</span>,<span class="cpp-number">250</span>,<span class="cpp-number">124</span>,<span class="cpp-number">123</span>,  <span class="cpp-number">5</span>,<span class="cpp-number">202</span>, <span class="cpp-number">38</span>,<span class="cpp-number">147</span>,<span class="cpp-number">118</span>,<span class="cpp-number">126</span>,<br>    <span class="cpp-number">255</span>, <span class="cpp-number">82</span>, <span class="cpp-number">85</span>,<span class="cpp-number">212</span>,<span class="cpp-number">207</span>,<span class="cpp-number">206</span>, <span class="cpp-number">59</span>,<span class="cpp-number">227</span>, <span class="cpp-number">47</span>, <span class="cpp-number">16</span>, <span class="cpp-number">58</span>, <span class="cpp-number">17</span>,<span class="cpp-number">182</span>,<span class="cpp-number">189</span>, <span class="cpp-number">28</span>, <span class="cpp-number">42</span>,<span class="cpp-number">223</span>,<span class="cpp-number">183</span>,<span class="cpp-number">170</span>,<span class="cpp-number">213</span>,<br>    <span class="cpp-number">119</span>,<span class="cpp-number">248</span>,<span class="cpp-number">152</span>,  <span class="cpp-number">2</span>, <span class="cpp-number">44</span>,<span class="cpp-number">154</span>,<span class="cpp-number">163</span>, <span class="cpp-number">70</span>,<span class="cpp-number">221</span>,<span class="cpp-number">153</span>,<span class="cpp-number">101</span>,<span class="cpp-number">155</span>,<span class="cpp-number">167</span>, <span class="cpp-number">43</span>,<span class="cpp-number">172</span>,  <span class="cpp-number">9</span>,<span class="cpp-number">129</span>, <span class="cpp-number">22</span>, <span class="cpp-number">39</span>,<span class="cpp-number">253</span>,<br><span class="cpp-number">19</span>, <span class="cpp-number">98</span>,<span class="cpp-number">108</span>,<span class="cpp-number">110</span>, <span class="cpp-number">79</span>,<span class="cpp-number">113</span>,<span class="cpp-number">224</span>,<span class="cpp-number">232</span>,<span class="cpp-number">178</span>,<span class="cpp-number">185</span>,<span class="cpp-number">112</span>,<span class="cpp-number">104</span>,<span class="cpp-number">218</span>,<span class="cpp-number">246</span>, <span class="cpp-number">97</span>,<span class="cpp-number">228</span>,<span class="cpp-number">251</span>, <span class="cpp-number">34</span>,<span class="cpp-number">242</span>,<span class="cpp-number">193</span>,<br>    <span class="cpp-number">238</span>,<span class="cpp-number">210</span>,<span class="cpp-number">144</span>, <span class="cpp-number">12</span>,<span class="cpp-number">191</span>,<span class="cpp-number">179</span>,<span class="cpp-number">162</span>,<span class="cpp-number">241</span>, <span class="cpp-number">81</span>, <span class="cpp-number">51</span>,<span class="cpp-number">145</span>,<span class="cpp-number">235</span>,<span class="cpp-number">249</span>, <span class="cpp-number">14</span>,<span class="cpp-number">239</span>,<span class="cpp-number">107</span>, <span class="cpp-number">49</span>,<span class="cpp-number">192</span>,<span class="cpp-number">214</span>, <span class="cpp-number">31</span>,<br>    <span class="cpp-number">181</span>,<span class="cpp-number">199</span>,<span class="cpp-number">106</span>,<span class="cpp-number">157</span>,<span class="cpp-number">184</span>, <span class="cpp-number">84</span>,<span class="cpp-number">204</span>,<span class="cpp-number">176</span>,<span class="cpp-number">115</span>,<span class="cpp-number">121</span>, <span class="cpp-number">50</span>, <span class="cpp-number">45</span>,<span class="cpp-number">127</span>,  <span class="cpp-number">4</span>,<span class="cpp-number">150</span>,<span class="cpp-number">254</span>,<span class="cpp-number">138</span>,<span class="cpp-number">236</span>,<span class="cpp-number">205</span>, <span class="cpp-number">93</span>,<br>    <span class="cpp-number">222</span>,<span class="cpp-number">114</span>, <span class="cpp-number">67</span>, <span class="cpp-number">29</span>, <span class="cpp-number">24</span>, <span class="cpp-number">72</span>,<span class="cpp-number">243</span>,<span class="cpp-number">141</span>,<span class="cpp-number">128</span>,<span class="cpp-number">195</span>, <span class="cpp-number">78</span>, <span class="cpp-number">66</span>,<span class="cpp-number">215</span>, <span class="cpp-number">61</span>,<span class="cpp-number">156</span>,<span class="cpp-number">180</span>,<br><br>    <span class="cpp-number">151</span>,<span class="cpp-number">160</span>,<span class="cpp-number">137</span>, <span class="cpp-number">91</span>, <span class="cpp-number">90</span>, <span class="cpp-number">15</span>,<span class="cpp-number">131</span>, <span class="cpp-number">13</span>,<span class="cpp-number">201</span>, <span class="cpp-number">95</span>, <span class="cpp-number">96</span>, <span class="cpp-number">53</span>,<span class="cpp-number">194</span>,<span class="cpp-number">233</span>,  <span class="cpp-number">7</span>,<span class="cpp-number">225</span>,<span class="cpp-number">140</span>, <span class="cpp-number">36</span>,<span class="cpp-number">103</span>, <span class="cpp-number">30</span>,<br><span class="cpp-number">69</span>,<span class="cpp-number">142</span>,  <span class="cpp-number">8</span>, <span class="cpp-number">99</span>, <span class="cpp-number">37</span>,<span class="cpp-number">240</span>, <span class="cpp-number">21</span>, <span class="cpp-number">10</span>, <span class="cpp-number">23</span>,<span class="cpp-number">190</span>,  <span class="cpp-number">6</span>,<span class="cpp-number">148</span>,<span class="cpp-number">247</span>,<span class="cpp-number">120</span>,<span class="cpp-number">234</span>, <span class="cpp-number">75</span>,  <span class="cpp-number">0</span>, <span class="cpp-number">26</span>,<span class="cpp-number">197</span>, <span class="cpp-number">62</span>,<br><span class="cpp-number">94</span>,<span class="cpp-number">252</span>,<span class="cpp-number">219</span>,<span class="cpp-number">203</span>,<span class="cpp-number">117</span>, <span class="cpp-number">35</span>, <span class="cpp-number">11</span>, <span class="cpp-number">32</span>, <span class="cpp-number">57</span>,<span class="cpp-number">177</span>, <span class="cpp-number">33</span>, <span class="cpp-number">88</span>,<span class="cpp-number">237</span>,<span class="cpp-number">149</span>, <span class="cpp-number">56</span>, <span class="cpp-number">87</span>,<span class="cpp-number">174</span>, <span class="cpp-number">20</span>,<span class="cpp-number">125</span>,<span class="cpp-number">136</span>,<br>    <span class="cpp-number">171</span>,<span class="cpp-number">168</span>, <span class="cpp-number">68</span>,<span class="cpp-number">175</span>, <span class="cpp-number">74</span>,<span class="cpp-number">165</span>, <span class="cpp-number">71</span>,<span class="cpp-number">134</span>,<span class="cpp-number">139</span>, <span class="cpp-number">48</span>, <span class="cpp-number">27</span>,<span class="cpp-number">166</span>, <span class="cpp-number">77</span>,<span class="cpp-number">146</span>,<span class="cpp-number">158</span>,<span class="cpp-number">231</span>, <span class="cpp-number">83</span>,<span class="cpp-number">111</span>,<span class="cpp-number">229</span>,<span class="cpp-number">122</span>,<br><span class="cpp-number">60</span>,<span class="cpp-number">211</span>,<span class="cpp-number">133</span>,<span class="cpp-number">230</span>,<span class="cpp-number">220</span>,<span class="cpp-number">105</span>, <span class="cpp-number">92</span>, <span class="cpp-number">41</span>, <span class="cpp-number">55</span>, <span class="cpp-number">46</span>,<span class="cpp-number">245</span>, <span class="cpp-number">40</span>,<span class="cpp-number">244</span>,<span class="cpp-number">102</span>,<span class="cpp-number">143</span>, <span class="cpp-number">54</span>, <span class="cpp-number">65</span>, <span class="cpp-number">25</span>, <span class="cpp-number">63</span>,<span class="cpp-number">161</span>,<br> <span class="cpp-number">1</span>,<span class="cpp-number">216</span>, <span class="cpp-number">80</span>, <span class="cpp-number">73</span>,<span class="cpp-number">209</span>, <span class="cpp-number">76</span>,<span class="cpp-number">132</span>,<span class="cpp-number">187</span>,<span class="cpp-number">208</span>, <span class="cpp-number">89</span>, <span class="cpp-number">18</span>,<span class="cpp-number">169</span>,<span class="cpp-number">200</span>,<span class="cpp-number">196</span>,<span class="cpp-number">135</span>,<span class="cpp-number">130</span>,<span class="cpp-number">116</span>,<span class="cpp-number">188</span>,<span class="cpp-number">159</span>, <span class="cpp-number">86</span>,<br>    <span class="cpp-number">164</span>,<span class="cpp-number">100</span>,<span class="cpp-number">109</span>,<span class="cpp-number">198</span>,<span class="cpp-number">173</span>,<span class="cpp-number">186</span>,  <span class="cpp-number">3</span>, <span class="cpp-number">64</span>, <span class="cpp-number">52</span>,<span class="cpp-number">217</span>,<span class="cpp-number">226</span>,<span class="cpp-number">250</span>,<span class="cpp-number">124</span>,<span class="cpp-number">123</span>,  <span class="cpp-number">5</span>,<span class="cpp-number">202</span>, <span class="cpp-number">38</span>,<span class="cpp-number">147</span>,<span class="cpp-number">118</span>,<span class="cpp-number">126</span>,<br>    <span class="cpp-number">255</span>, <span class="cpp-number">82</span>, <span class="cpp-number">85</span>,<span class="cpp-number">212</span>,<span class="cpp-number">207</span>,<span class="cpp-number">206</span>, <span class="cpp-number">59</span>,<span class="cpp-number">227</span>, <span class="cpp-number">47</span>, <span class="cpp-number">16</span>, <span class="cpp-number">58</span>, <span class="cpp-number">17</span>,<span class="cpp-number">182</span>,<span class="cpp-number">189</span>, <span class="cpp-number">28</span>, <span class="cpp-number">42</span>,<span class="cpp-number">223</span>,<span class="cpp-number">183</span>,<span class="cpp-number">170</span>,<span class="cpp-number">213</span>,<br>    <span class="cpp-number">119</span>,<span class="cpp-number">248</span>,<span class="cpp-number">152</span>,  <span class="cpp-number">2</span>, <span class="cpp-number">44</span>,<span class="cpp-number">154</span>,<span class="cpp-number">163</span>, <span class="cpp-number">70</span>,<span class="cpp-number">221</span>,<span class="cpp-number">153</span>,<span class="cpp-number">101</span>,<span class="cpp-number">155</span>,<span class="cpp-number">167</span>, <span class="cpp-number">43</span>,<span class="cpp-number">172</span>,  <span class="cpp-number">9</span>,<span class="cpp-number">129</span>, <span class="cpp-number">22</span>, <span class="cpp-number">39</span>,<span class="cpp-number">253</span>,<br><span class="cpp-number">19</span>, <span class="cpp-number">98</span>,<span class="cpp-number">108</span>,<span class="cpp-number">110</span>, <span class="cpp-number">79</span>,<span class="cpp-number">113</span>,<span class="cpp-number">224</span>,<span class="cpp-number">232</span>,<span class="cpp-number">178</span>,<span class="cpp-number">185</span>,<span class="cpp-number">112</span>,<span class="cpp-number">104</span>,<span class="cpp-number">218</span>,<span class="cpp-number">246</span>, <span class="cpp-number">97</span>,<span class="cpp-number">228</span>,<span class="cpp-number">251</span>, <span class="cpp-number">34</span>,<span class="cpp-number">242</span>,<span class="cpp-number">193</span>,<br>    <span class="cpp-number">238</span>,<span class="cpp-number">210</span>,<span class="cpp-number">144</span>, <span class="cpp-number">12</span>,<span class="cpp-number">191</span>,<span class="cpp-number">179</span>,<span class="cpp-number">162</span>,<span class="cpp-number">241</span>, <span class="cpp-number">81</span>, <span class="cpp-number">51</span>,<span class="cpp-number">145</span>,<span class="cpp-number">235</span>,<span class="cpp-number">249</span>, <span class="cpp-number">14</span>,<span class="cpp-number">239</span>,<span class="cpp-number">107</span>, <span class="cpp-number">49</span>,<span class="cpp-number">192</span>,<span class="cpp-number">214</span>, <span class="cpp-number">31</span>,<br>    <span class="cpp-number">181</span>,<span class="cpp-number">199</span>,<span class="cpp-number">106</span>,<span class="cpp-number">157</span>,<span class="cpp-number">184</span>, <span class="cpp-number">84</span>,<span class="cpp-number">204</span>,<span class="cpp-number">176</span>,<span class="cpp-number">115</span>,<span class="cpp-number">121</span>, <span class="cpp-number">50</span>, <span class="cpp-number">45</span>,<span class="cpp-number">127</span>,  <span class="cpp-number">4</span>,<span class="cpp-number">150</span>,<span class="cpp-number">254</span>,<span class="cpp-number">138</span>,<span class="cpp-number">236</span>,<span class="cpp-number">205</span>, <span class="cpp-number">93</span>,<br>    <span class="cpp-number">222</span>,<span class="cpp-number">114</span>, <span class="cpp-number">67</span>, <span class="cpp-number">29</span>, <span class="cpp-number">24</span>, <span class="cpp-number">72</span>,<span class="cpp-number">243</span>,<span class="cpp-number">141</span>,<span class="cpp-number">128</span>,<span class="cpp-number">195</span>, <span class="cpp-number">78</span>, <span class="cpp-number">66</span>,<span class="cpp-number">215</span>, <span class="cpp-number">61</span>,<span class="cpp-number">156</span>,<span class="cpp-number">180</span><br>};<br><br><span class="cpp-comment">// Quintic curve</span><br><span class="cpp-keyword">float</span> qCurve(<span class="cpp-keyword">float</span> t)<br>{<br>    <span class="cpp-keyword">return</span> t * t * t * (t * (t * <span class="cpp-number">6</span>.0f - <span class="cpp-number">15</span>.0f) + <span class="cpp-number">10</span>.0f);<br>}<br><br><span class="cpp-comment">// Linear interpolation</span><br><span class="cpp-keyword">float</span> lerp(<span class="cpp-keyword">float</span> a, <span class="cpp-keyword">float</span> b, <span class="cpp-keyword">float</span> t)<br>{<br>    <span class="cpp-keyword">return</span> a + t * (b - a);<br>}<br><br><span class="cpp-keyword">float</span> grad(<span class="cpp-keyword">int</span> hash, <span class="cpp-keyword">float</span> x, <span class="cpp-keyword">float</span> y)<br>{<br>    hash &amp;= <span class="cpp-number">7</span>;  <span class="cpp-comment">// convert low 3 bits of hash code</span><br>    <span class="cpp-keyword">float</span> s = (hash &lt; <span class="cpp-number">4</span>) ? x : y;  <span class="cpp-comment">// into 8 simple gradient directions</span><br>    <span class="cpp-keyword">float</span> t = (hash &lt; <span class="cpp-number">4</span>) ? y : x;  <span class="cpp-comment">// and compute the dot product with (x, y)</span><br>    <span class="cpp-keyword">return</span> ((hash &amp; <span class="cpp-number">1</span>) ? -s : s) + ((hash &amp; <span class="cpp-number">2</span>) ? -<span class="cpp-number">2</span>.0f * t : <span class="cpp-number">2</span>.0f * t);<br>}<br><br></pre></div><!–ENDSCRIPT–> <br><br><!–EDIT–><span class=editedby><!–/EDIT–>[Edited by - y2kiah on June 2, 2009 7:21:17 PM]<!–EDIT–></span><!–/EDIT–>;  Thanks,But it seems to miss the grad() function. Would it be possible to see it?thanks   ;  Sorry, I edited my previous post and added the grad function to the end of the code block.   