# unity3d 图形吧 之 场景中画圆

﻿﻿

2d就不介绍了，相对简单一些，对于3d的内容，我们先来看一看数学中的一个题和答案，这样就很容易理解程序了。

using UnityEngine;
using System.Collections;
using SunGuangDong.Math;

[ExecuteInEditMode]
public class DrawCircleMono : MonoBehaviour {
private Vector3 m_Normal ;

// Use this for initialization
void Start () {
m_Normal = new Vector3 (0, 1, 0);
}

void OnDrawGizmos()
{
Circle2d circle2 = new Circle2d(transform.position, 5);
DrawCircle(ref circle2, Color.red);

Vector3 center = new Vector3 (transform.position.x, transform.position.y + 0.2f, transform.position.z);
Circle3d circle3 = new Circle3d(ref center,ref m_Normal, 5);
DrawCircle (circle3, Color.red, 40);
}

protected void DrawCircle(ref Circle2d circle, Color color)
{
int count = 40;
float delta = 2f * Mathf.PI / count;
Vector3 prev = circle.Eval(0);

Color tempColor = Gizmos.color;
Gizmos.color = color;

for (int i = 1; i <= count; ++i)
{
Vector3 curr = circle.Eval(i * delta);
Gizmos.DrawLine(prev, curr);
prev = curr;
}

Gizmos.color = tempColor;
}

protected void DrawCircle(Vector2 center, float radius, Color color)
{
Circle2d circle = new Circle2d(ref center, radius);
int count = 40;
float delta = 2f * Mathf.PI / count;
Vector3 prev = circle.Eval(0);

Color tempColor = Gizmos.color;
Gizmos.color = color;

for (int i = 1; i <= count; ++i)
{
Vector3 curr = circle.Eval(i * delta);
Gizmos.DrawLine(prev, curr);
prev = curr;
}

Gizmos.color = tempColor;
}

protected void DrawCircle(Circle3d circle, Color color, int count = 20)
{
float delta = 2f * Mathf.PI / count;
Vector3 prev = circle.Eval(0);

Color tempColor = Gizmos.color;
Gizmos.color = color;

for (int i = 1; i <= count; ++i)
{
Vector3 curr = circle.Eval(i * delta);
Gizmos.DrawLine(prev, curr);
prev = curr;
}

Gizmos.color = tempColor;
}
}

namespace SunGuangDong.Math
{
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;

[StructLayout(LayoutKind.Sequential)]
public struct Circle2d
{
public Vector2 Center;
public Circle2d(ref Vector2 center, float radius)
{
this.Center = center;
}

{
this.Center = center;
}

public float CalcPerimeter()
{
}

public float CalcArea()
{
}

public Vector2 Eval(float t)
{
return new Vector2(this.Center.x + (this.Radius * Mathf.Cos(t)), this.Center.y + (this.Radius * Mathf.Sin(t)));
}

public Vector2 Eval(float t, float radius)
{
return new Vector2(this.Center.x + (radius * Mathf.Cos(t)), this.Center.y + (radius * Mathf.Sin(t)));
}
public bool Contains(ref Vector2 point)
{
Vector2 vector = point - this.Center;
}

public bool Contains(Vector2 point)
{
Vector2 vector = point - this.Center;
}

public void Include(ref Circle2d circle)
{
Vector2 vector = circle.Center - this.Center;
float num = vector.magnitude;
float num3 = num2 * num2;
if (num3 >= num)
{
if (num2 >= 0f)
{
this = circle;
}
}
else
{
float num4 = Mathf.Sqrt(num);
if (num4 > 1E-05f)
{
float num5 = (num4 + num2) / (2f * num4);
this.Center += (Vector2) (num5 * vector);
}
}
}

public void Include(Circle2d circle)
{
this.Include(ref circle);
}
}
}

namespace SunGuangDong.Math
{
using System;
using System.Runtime.InteropServices;
using UnityEngine;

[StructLayout(LayoutKind.Sequential)]
public struct Circle3d
{
public Vector3 Center;
public Vector3 Axis0;
public Vector3 Axis1;
public Vector3 Normal;

public Circle3d(ref Vector3 center, ref Vector3 normal, float radius)
{
this.Center = center;
this.Normal = normal;
Vector3ex.CreateOrthonormalBasis(out this.Axis0, out this.Axis1, ref this.Normal);
}

public Circle3d(Vector3 center, Vector3 normal, float radius)
{
this.Center = center;
this.Normal = normal;
Vector3ex.CreateOrthonormalBasis(out this.Axis0, out this.Axis1, ref this.Normal);
}

public float CalcPerimeter()
{
}

public float CalcArea()
{
}

public Vector3 Eval(float t)
{
return (this.Center + ((Vector3) (this.Radius * ((Mathf.Cos(t) * this.Axis0) + (Mathf.Sin(t) * this.Axis1)))));
}

public Vector3 Eval(float t, float radius)
{
return (this.Center + ((Vector3) (radius * ((Mathf.Cos(t) * this.Axis0) + (Mathf.Sin(t) * this.Axis1)))));
}
}
}

namespace SunGuangDong.Math
{
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using UnityEngine;

public static class Vector3ex
{
public static readonly Vector3 Zero = new Vector3(0f, 0f, 0f);

public static void CreateOrthonormalBasis(out Vector3 u, out Vector3 v, ref Vector3 w)
{
if (Mathf.Abs(w.x) >= Mathf.Abs(w.y))
{
float num = Mathfex.InvSqrt((w.x * w.x) + (w.z * w.z));
u.x = -w.z * num;
u.y = 0f;
u.z = w.x * num;
v.x = w.y * u.z;
v.y = (w.z * u.x) - (w.x * u.z);
v.z = -w.y * u.x;
}
else
{
float num2 = Mathfex.InvSqrt((w.y * w.y) + (w.z * w.z));
u.x = 0f;
u.y = w.z * num2;
u.z = -w.y * num2;
v.x = (w.y * u.z) - (w.z * u.y);
v.y = -w.x * u.z;
v.z = w.x * u.y;
}
}
}
}

namespace SunGuangDong.Math
{
using System;
using System.Runtime.InteropServices;
using UnityEngine;

public static class Mathfex
{
public const float Pi = 3.141593f;

public static float InvSqrt(float value)
{
if (value != 0f)
{
return (1f / Mathf.Sqrt(value));
}
return 0f;
}
}
}