当前位置: 代码迷 >> Windows Mobile >> 作圆有关问题,求改修,指导
  详细解决方案

作圆有关问题,求改修,指导

热度:841   发布时间:2013-02-26 00:00:00.0
作圆问题,求改修,指导
求助各位大神,为何不能准确的拖拽出圆,是算法有问题么?
(需求,按下屏幕时是中心点,拖拽,当松开的时候是第二个点。以中心点到第二个点为半径作圆)

C# code
using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Windows;using System.Windows.Controls;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Animation;using System.Windows.Shapes;using Microsoft.Phone.Controls;namespace Shape_draw_Circle{    public partial class MainPage : PhoneApplicationPage    {        // 构造函数        Random rand = new Random();        bool isDrawing, isDragging;        Path path;        EllipseGeometry ellipseGeo;        public MainPage()        {            InitializeComponent();        }        protected override void OnManipulationStarted(ManipulationStartedEventArgs args)        {            if (isDrawing || isDragging)            {                return;            }            if (args.OriginalSource is Path)            {                ellipseGeo = (args.OriginalSource as Path).Data as EllipseGeometry;                isDragging = true;                args.ManipulationContainer = ContentPanel;                args.Handled = true;            }            else if (args.OriginalSource == ContentPanel)            {                ellipseGeo = new EllipseGeometry();                ellipseGeo.Center = args.ManipulationOrigin;                path = new Path();                path.Stroke = this.Resources["PhoneForegroundBrush"] as Brush;                path.Data = ellipseGeo;                ContentPanel.Children.Add(path);                isDrawing = true;                args.Handled = true;            }            base.OnManipulationStarted(args);        }        protected override void OnManipulationDelta(ManipulationDeltaEventArgs args)        {            if (isDragging)            {                Point center = ellipseGeo.Center;                center.X += args.DeltaManipulation.Translation.X;                center.Y += args.DeltaManipulation.Translation.Y;                args.Handled = true;            }            else if (isDrawing)            {                Point translation = args.CumulativeManipulation.Translation;                //double radius = Math.Max(Math.Abs(translation.X),                //                            Math.Abs(translation.Y));                //ellipseGeo.RadiusX = radius;                //ellipseGeo.RadiusY = radius;                      //BY YZX                //Math.Sqrt  开根                //Math.Pow   求幂                //radius  求圆的半径                double radius =Math.Sqrt(Math.Pow((translation.X - ellipseGeo.Center.X),2)+Math.Pow((translation.Y - ellipseGeo.Center.Y),2));                ellipseGeo.RadiusX = radius;                ellipseGeo.RadiusY = radius;                args.Handled = true;            }            base.OnManipulationDelta(args);        }        protected override void OnManipulationCompleted(ManipulationCompletedEventArgs args)        {            if (isDragging)            {                isDragging = false;                args.Handled = true;            }            else if (isDrawing)            {                Color clr = Color.FromArgb(255, 0, 0, 0);                Color clr2 = Color.FromArgb(255, 5, 5, 5);                //path.Fill = new SolidColorBrush(clr);                path.Stroke = new SolidColorBrush(clr2);                path.StrokeThickness = 10;                isDrawing = false;                args.Handled = true;            }            base.OnManipulationCompleted(args);        }    }}


------解决方案--------------------------------------------------------
楼主的代码风格。。。。。
先说建议吧,您这个又是DRAWING又是DRUGGING的,应该用状态机实现,至少写个枚举代表状态吧。
另外你一直用OnManipulationDelta来记录最新的电位变化,但是OnManipulationCompleted却没有记录点位。应该是这个问题引起的。
  相关解决方案