Advertisement
Guest User

Flower

a guest
Oct 23rd, 2012
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.35 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. using Ephere.Geometry;
  5.  
  6. /// Source: http://demonstrations.wolfram.com/VirtualFlowersWithCrispatePetals/
  7.  
  8. namespace Flowers
  9. {
  10.     public static class FlowerUtils
  11.     {
  12.         public static PolygonMesh CreateFlower(double details, int innerSegments, double r0, double r1,
  13.             double c, double d, double a1, double p, double growth, double aa, double b, double a)
  14.         {
  15.             const double tMax = 6.255;
  16.             int silhouetteVertCount = (int)(tMax / details);
  17.             int numVerts = innerSegments * silhouetteVertCount + 1;
  18.             Vector3[] vertices = new Vector3[numVerts];
  19.             vertices[vertices.Length - 1] = new Vector3(0, 0, 0);
  20.             PolygonMesh.Polygon[] faces = new PolygonMesh.Polygon[numVerts - 1];
  21.  
  22.             double angleStep = 2 * Math.PI * details / tMax;
  23.  
  24.             for (int i = 0; i < silhouetteVertCount; i++)
  25.             {
  26.                 double angle = i * angleStep;
  27.                 double radius = r0 + (r1 * Math.Pow(Math.Abs(Math.Sin(c * angle * 0.5)), d));
  28.                 double x = radius * Math.Cos(angle);
  29.                 double y = radius * Math.Sin(angle);
  30.                 double rad = Math.Pow(x, 2) + Math.Pow(y, 2);
  31.                 double z = (1 + rad) * a1 * Math.Sin(p * i);
  32.  
  33.                 vertices[i] = new Vector3((float)x, (float)y, (float)z) * (float)growth;
  34.             }
  35.  
  36.             if (innerSegments == 1)
  37.                 for (int i = 0; i < faces.Length; i++)
  38.                     faces[i] = new PolygonMesh.Polygon(new int[] { vertices.Length - 1, i, (i + 1) % faces.Length });
  39.            
  40.             else
  41.                 for (int i = 0; i < innerSegments ; i++)
  42.                 {
  43.                     float discount = (i + 1.25f) / (innerSegments + 1);
  44.  
  45.                     for (int j = 0; j < silhouetteVertCount; j++)
  46.                     {
  47.                         if (i < innerSegments - 1)
  48.                             vertices[j + (i + 1) * silhouetteVertCount] = vertices[j] * discount;
  49.  
  50.                         int k = (i + 1) * silhouetteVertCount;
  51.                         int l = i * silhouetteVertCount;
  52.  
  53.                         if (i == 0)
  54.                             faces[j] = new PolygonMesh.Polygon(new int[] { vertices.Length - 1, k + j, ((j + 1) % silhouetteVertCount) + silhouetteVertCount});
  55.  
  56.                         else if (i < innerSegments - 1)
  57.                             faces[i * silhouetteVertCount + j] = new PolygonMesh.Polygon(
  58.                                 new int[] { l + j, k + j, ((k + 1 + j) % silhouetteVertCount) + k, ((l + 1 + j) % silhouetteVertCount) + l });
  59.  
  60.                         else
  61.                             faces[i * silhouetteVertCount + j] = new PolygonMesh.Polygon(
  62.                                 new int[] { l + j, j, (j + 1) % silhouetteVertCount, ((l + 1 + j) % silhouetteVertCount) + l });
  63.                                
  64.                     }
  65.                 }
  66.  
  67.             for (int i = 0; i < vertices.Length; i++)
  68.             {
  69.                 double dist = vertices[i].Length;
  70.                 vertices[i].z += (float)(aa * Math.Exp(-b * Math.Pow(dist, 1.5)) * Math.Pow(dist, a));
  71.             }
  72.  
  73.             foreach (var face in faces)
  74.                 face.SmoothingGroup = 1;
  75.  
  76.             return new PolygonMesh(vertices, faces);
  77.         }
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement