A easy way to recognize mouse and finger strokes on screen. Recod every point of the storke and check the current point and last point's relative direction. The diection lies on 0~360 degree. Separate 0~360 into 8 even sectors, every sector covers 45 degree range ; Up : -22.5~22.5, Upright : 22.5~67.5, Right : 67.5~112.5, Rightdown : 112.5~157.5, Down : 157.5~202.5, Downleft : 202.5~247.5, Left : 247.5~292.5, Upleft : 292.5~337.5. Give the 8 direcions numbers 1~8respectively. Record the direction when direction changes. For example, when write a storke toward 30 degree then turn the direction to 120 degree. You will get a sequence number 24. Then according to the number to check which symbol is written. Usually handwriting doesn't turn the direction sharply so the sequence number may be many. For example, when write L, the sequence number may be 53, 543. When write N in one stroke, the sequence number may be 141, 13431, 1341, 1431. You should build the database for any posibilities. This way can't identify the lenght of the stoke so if the two symbols have different length of stokes but turn the same directions, theny may not be identified. You should specially handle the case since this is just a simple symsem. Even though this way is enough to recognitize many symbols. The following demo shows >, <, 一, | and N handwriting recognition.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class drawline : MonoBehaviour {
public Vector3 currentPosition;
public Vector3 lastPosition;
float angle;
public bool removePoints =false ;
float interval = 0.1f;
public int pointNUmber = 0;
string direction;
string directionChain = "0";
int symbolCardNum = 0 ;
GameObject cardBack;
// Use this for initialization
void Start () {
cardBack = Instantiate(Resources.Load("prefab/cardBack", typeof(GameObject))) as GameObject;
cardBack.transform.position = new Vector3(-20, 0, 0);
}
// Update is called once per frame
void Update () {
drawLine();
assignPointNumber();
}
void drawLine()
{
if (Input.GetMouseButton(0))
{
interval -= 0.01f;
Vector3 mouse_p = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10));
if (Vector3.Distance(mouse_p, currentPosition) > 0.5 && interval < 0) {
GameObject point = Instantiate(Resources.Load("prefab/point", typeof(GameObject))) as GameObject;
point.transform.position = new Vector3(mouse_p.x, mouse_p.y, mouse_p.z);
interval = 0.1f;
}
}
if (Input.GetMouseButtonUp(0))
{
pointNUmber = 0;
directionCheck();
directionChain = "0";
}
}
void assignPointNumber()
{
angle = Vector3.Angle(currentPosition - lastPosition, transform.up);
if(0< angle && angle < 22.5)
{
direction = "1";
}
if (22.5 <= angle && angle < 67.5)
{
if(currentPosition.x > lastPosition.x) {
direction = "2";
}
else {
direction = "8";
}
}
if (67.5 <= angle && angle < 112.5)
{
if (currentPosition.x > lastPosition.x) {
direction = "3";
}
else {
direction = "7";
}
}
if (112.5 <= angle && angle < 157.5)
{
if (currentPosition.x > lastPosition.x) {
direction = "4";
}
else {
direction = "6";
}
}
if (157.5 <= angle && angle < 180)
{
direction = "5";
}
if(directionChain.Substring(directionChain.Length-1, 1) != direction && pointNUmber >=3)
{
directionChain += direction;
Debug.Log("directionChain : " + directionChain);
}
}
public void clear(bool sweep)
{
removePoints = sweep;
directionChain = "0";
symbolCardNum = 0;
cardBack.transform.position = new Vector3(-20, 0, 0);
}
void directionCheck()
{
if(directionChain == "0525" || directionChain == "05215" || directionChain == "0515" || directionChain == "05125" || directionChain == "0521")
{
GameObject symbolCard = Instantiate(Resources.Load("prefab/1-inscription", typeof(GameObject))) as GameObject;
symbolCard.transform.position = Vector3.Lerp(new Vector3(-11.59f + 2.55f*(symbolCardNum%10-1), 7.43f - 2.55f * (int)(symbolCardNum / 10), 5), new Vector3(0, 7.43f - 2.55f * (int)(symbolCardNum / 10), 5), 0.1f);
cardBack.transform.position = symbolCard.transform.position;
cardBack.transform.position = symbolCard.transform.position;
symbolCardNum += 1;
}
if (directionChain == "074" || directionChain == "073" || directionChain == "0743" || directionChain == "0743" || directionChain == "063"
|| directionChain == "0643" || directionChain == "0634")
{
GameObject symbolCard = Instantiate(Resources.Load("prefab/2-inscription", typeof(GameObject))) as GameObject;
symbolCard.transform.position = Vector3.Lerp(new Vector3(-11.59f + 2.55f * (symbolCardNum % 10 - 1), 7.43f - 2.55f * (int)(symbolCardNum / 10), 5), new Vector3(0, 7.43f - 2.55f * (int)(symbolCardNum / 10), 5), 0.1f);
cardBack.transform.position = symbolCard.transform.position;
symbolCardNum += 1;
}
if (directionChain == "0437" || directionChain == "037" || directionChain == "047" || directionChain == "0467" || directionChain == "046" || directionChain == "036" || directionChain == "0367" || directionChain == "0476")
{
GameObject symbolCard = Instantiate(Resources.Load("prefab/3-inscription", typeof(GameObject))) as GameObject;
symbolCard.transform.position = Vector3.Lerp(new Vector3(-11.59f + 2.55f * (symbolCardNum % 10 - 1), 7.43f - 2.55f * (int)(symbolCardNum/10) , 5), new Vector3(0, 7.43f - 2.55f * (int)(symbolCardNum / 10), 5), 0.1f);
cardBack.transform.position = symbolCard.transform.position;
symbolCardNum += 1;
}
if (directionChain == "05" || directionChain == "075")
{
GameObject symbolCard = Instantiate(Resources.Load("prefab/4-inscription", typeof(GameObject))) as GameObject;
symbolCard.transform.position = Vector3.Lerp(new Vector3(-11.59f + 2.55f * (symbolCardNum % 10 - 1), 7.43f - 2.55f * (int)(symbolCardNum / 10), 5), new Vector3(0, 7.43f - 2.55f * (int)(symbolCardNum / 10), 5), 0.1f);
cardBack.transform.position = symbolCard.transform.position;
symbolCardNum += 1;
}
if (directionChain == "03")
{
GameObject symbolCard = Instantiate(Resources.Load("prefab/5-inscription", typeof(GameObject))) as GameObject;
symbolCard.transform.position = Vector3.Lerp(new Vector3(-11.59f + 2.55f * (symbolCardNum % 10 - 1), 7.43f - 2.55f * (int)(symbolCardNum / 10), 5), new Vector3(0, 7.43f - 2.55f * (int)(symbolCardNum / 10), 5), 0.1f);
cardBack.transform.position = symbolCard.transform.position;
symbolCardNum += 1;
}
}
}