Beispiel #1
0
        private void OnElementChanged(object sender, EventArgs args)
        {
            RoutedEventArgs routedArgs = args as RoutedEventArgs;

            if (routedArgs != null)
            {
                routedArgs.Handled = true;
            }
            if (_ignoreControls)
            {
                return;
            }

            // determine side count and orientation
            int sides = (SquareToggle.IsChecked == true ? 4 : 6);
            PolygonOrientation orientation = (OnEdgeToggle.IsChecked == true ?
                                              PolygonOrientation.OnEdge : PolygonOrientation.OnVertex);

            // determine side length
            double length = (double)ElementUpDown.Value;

            Element = new RegularPolygon(length, sides, orientation);

            // determine valid & default controls
            EnableControls(sides, orientation);

            // recalculate grid accordingly
            OnGridChanged(sender, args);
        }
Beispiel #2
0
        internal void UpdatePolygons(Size layoutSize)
        {
            // determine side count and orientation
            int sides = (int)SidesUpDown.Value;
            PolygonOrientation orientation = (OnEdgeToggle.IsChecked == true ?
                                              PolygonOrientation.OnEdge : PolygonOrientation.OnVertex);

            // compute side length based on layout size and side count
            double layout = Math.Min(layoutSize.Width, layoutSize.Height);
            double length = 2.5 * layout / sides;

            StandardPolygon = new RegularPolygon(length, sides, orientation);

            // determine inflation relative to standard polygon
            int inflation = (int)InflationUpDown.Value;

            InflatedPolygon = StandardPolygon.Inflate(inflation);
        }
        private void EnableControls(int sides, PolygonOrientation orientation)
        {
            _ignoreControls = true;

            bool onEdge = (orientation == PolygonOrientation.OnEdge);

            RowLeftToggle.IsEnabled  = !onEdge;
            RowRightToggle.IsEnabled = !onEdge;
            if (!onEdge)
            {
                RowRightToggle.IsChecked = true;
            }

            if (sides == 4)
            {
                VertexNeighborsToggle.IsEnabled = true;

                ShiftNoneToggle.IsEnabled  = onEdge;
                ColumnUpToggle.IsEnabled   = !onEdge;
                ColumnDownToggle.IsEnabled = !onEdge;
                if (onEdge)
                {
                    ShiftNoneToggle.IsChecked = true;
                }
            }
            else
            {
                VertexNeighborsToggle.IsEnabled = false;

                ShiftNoneToggle.IsEnabled  = false;
                ColumnUpToggle.IsEnabled   = onEdge;
                ColumnDownToggle.IsEnabled = onEdge;
                if (onEdge)
                {
                    ColumnDownToggle.IsChecked = true;
                }
            }

            _ignoreControls = false;
        }
        private void UpdatePolygons()
        {
            // determine side count and integral parameters
            int sides = (SquareToggle.IsChecked == true ? 4 : 6);
            PolygonOrientation orientation = (OnEdgeToggle.IsChecked == true ?
                                              PolygonOrientation.OnEdge : PolygonOrientation.OnVertex);
            bool vertexNeighbors = (sides <= 4 ? (VertexNeighborsToggle.IsChecked == true) : false);

            // adjust side length based on side count
            double length = (double)(160.0 / sides);

            StandardPolygon = new RegularPolygon(length, sides, orientation, vertexNeighbors);
            InsetPolygon    = StandardPolygon.Inflate(-3.0);

            // store inset vertices as stream geometry
            InsetGeometry = new StreamGeometry();
            using (StreamGeometryContext context = InsetGeometry.Open())
                InsetPolygon.Draw(context, PointD.Empty, true);
            InsetGeometry.Freeze();

            // determine valid & default controls
            EnableControls(sides, orientation);
        }
Beispiel #5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="RegularPolygon"/> class with the specified
        /// side length, number of sides, orientation, and vertex neighbors flag.</summary>
        /// <param name="length">
        /// The length of each side of the <see cref="RegularPolygon"/>.</param>
        /// <param name="sides">
        /// The number of sides of the <see cref="RegularPolygon"/>.</param>
        /// <param name="orientation">
        /// The orientation of the <see cref="RegularPolygon"/>.</param>
        /// <param name="vertexNeighbors">
        ///  <c>true</c> if <see cref="RegularPolygon"/> shapes that share only a common vertex are
        /// considered neighbors; otherwise, <c>false</c>.</param>
        /// <exception cref="ArgumentOutOfRangeException"><para>
        /// <paramref name="length"/> is equal to or less than zero.
        /// </para><para>-or-</para><para>
        /// <paramref name="sides"/> is less than three.
        /// </para><para>-or-</para><para>
        /// <paramref name="vertexNeighbors"/> is <c>true</c>, and <paramref name="sides"/> is
        /// greater than four.</para></exception>
        /// <exception cref="InvalidEnumArgumentException">
        /// <paramref name="orientation"/> is not a valid <see cref="PolygonOrientation"/> value.
        /// </exception>

        public RegularPolygon(double length, int sides,
                              PolygonOrientation orientation, bool vertexNeighbors)
        {
            if (length <= 0.0)
            {
                ThrowHelper.ThrowArgumentOutOfRangeException(
                    "length", length, Strings.ArgumentNotPositive);
            }

            if (sides < 3)
            {
                ThrowHelper.ThrowArgumentOutOfRangeExceptionWithFormat(
                    "sides", sides, Strings.ArgumentLessValue, 3);
            }

            if (vertexNeighbors && sides > 4)
            {
                ThrowHelper.ThrowArgumentOutOfRangeException(
                    "vertexNeighbors", vertexNeighbors, Strings.ArgumentTrue);
            }

            Length          = length;
            Sides           = sides;
            Orientation     = orientation;
            VertexNeighbors = vertexNeighbors;

            // compute maximum neighbors on edges and vertices
            Connectivity = (vertexNeighbors ? 2 * sides : sides);

            // determine whether a top connection exists
            HasTopIndex = (vertexNeighbors || (sides % 2 == 0 ?
                                               orientation == PolygonOrientation.OnEdge :
                                               orientation == PolygonOrientation.OnVertex));

            // compute angle of one segment between vertices
            double angle, segment = (2.0 * Math.PI) / sides;

            // compute radii of circumscribed and inscribed circles
            OuterRadius = length / (2.0 * Math.Sin(segment / 2.0));
            InnerRadius = OuterRadius * Math.Cos(segment / 2.0);

            // compute angle of first vertex and check orientation
            switch (orientation)
            {
            case PolygonOrientation.OnEdge:
                angle = (sides % 2 == 0 ? segment : 0.0);
                break;

            case PolygonOrientation.OnVertex:
                angle = (sides % 2 == 0 ? 0.0 : segment);
                break;

            default:
                ThrowHelper.ThrowInvalidEnumArgumentException(
                    "orientation", (int)orientation, typeof(PolygonOrientation));
                angle = 0.0;
                break;
            }

            // halve angle and rotate 90° counter-clockwise
            angle = (angle - Math.PI) / 2.0;

            // compute and store vertex coordinates around center
            Vertices = new PointD[sides];
            for (int i = 0; i < sides; i++, angle += segment)
            {
                Vertices[i] = new PointD(
                    OuterRadius * Math.Cos(angle), OuterRadius * Math.Sin(angle));
            }

            // compute and store circumscribed rectangle
            Bounds = RectD.Circumscribe(Vertices);
        }
Beispiel #6
0
        /// <overloads>
        /// Initializes a new instance of the <see cref="RegularPolygon"/> class.</overloads>
        /// <summary>
        /// Initializes a new instance of the <see cref="RegularPolygon"/> class with the specified
        /// side length, number of sides, and orientation.</summary>
        /// <param name="length">
        /// The length of each side of the <see cref="RegularPolygon"/>.</param>
        /// <param name="sides">
        /// The number of sides of the <see cref="RegularPolygon"/>.</param>
        /// <param name="orientation">
        /// The orientation of the <see cref="RegularPolygon"/>.</param>
        /// <exception cref="ArgumentOutOfRangeException"><para>
        /// <paramref name="length"/> is equal to or less than zero.
        /// </para><para>-or-</para><para>
        /// <paramref name="sides"/> is less than three.</para></exception>
        /// <exception cref="InvalidEnumArgumentException">
        /// <paramref name="orientation"/> is not a valid <see cref="PolygonOrientation"/> value.
        /// </exception>
        /// <remarks>
        /// The <see cref="VertexNeighbors"/> property is initialized to <c>false</c>.</remarks>

        public RegularPolygon(double length, int sides, PolygonOrientation orientation) :
            this(length, sides, orientation, false)
        {
        }