locked
moving circle in canvas RRS feed

  • Question

  • i'm trying to move a green circle in side a canvas and when the circle become inside the red circle it will change it's color.

    the problem is:

    1-I can't find the position (x,y) of the green circle after move

    2- how can I now if the green circle in side the red one?

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <Canvas x:Name="maincanvas"  HorizontalAlignment="Left" Height="436" Margin="125,122,0,0" VerticalAlignment="Top" Width="1130" Background="#FFEAE2E2">
                <Ellipse x:Name="redcircle"  Fill="Red" Height="182" Canvas.Left="577" Stroke="Black" Canvas.Top="141" Width="188"/>
                <Ellipse x:Name="greencircle" Fill="Green" Height="100" Canvas.Left="225" Stroke="Black" Canvas.Top="172" Width="122" ManipulationMode="All" />
            </Canvas>
        </Grid>

    Public NotInheritable Class MainPage
        Inherits Page
    
        Dim dragTranslation As New TranslateTransform
    
        Public Sub New()
    
            InitializeComponent()
    
            AddHandler greencircle.ManipulationDelta, AddressOf Me.Drag_ManipulationDelta
            greencircle.RenderTransform = Me.dragTranslation
    
        End Sub
    
        Private Sub Drag_ManipulationDelta(sender As Object, e As ManipulationDeltaRoutedEventArgs)
           
    
            Dim transform = greencircle.TransformToVisual(maincanvas)
            Dim p As Point
            p.X = 0
            p.Y = 0
            Dim point As Point = transform.TransformPoint(p)
    
    
    
            If (maincanvas.Width > (greencircle.Width + point.X + e.Delta.Translation.X) AndAlso (0 < (point.X + e.Delta.Translation.X))) Then
                dragTranslation.X += e.Delta.Translation.X
            End If
    
            If (maincanvas.Height > (greencircle.Height + point.Y + e.Delta.Translation.Y)) AndAlso (0 < (point.Y + e.Delta.Translation.Y)) Then
                dragTranslation.Y += e.Delta.Translation.Y
            End If
    
    
            Dim x As Double = CDbl(greencircle.GetValue(Canvas.LeftProperty))
            Dim y As Double = CDbl(greencircle.GetValue(Canvas.TopProperty))
    
    
            e.Handled = True
    
    
        End Sub
    
    End Class

    Saturday, December 14, 2013 10:30 AM

Answers

  • Hi imr2k1,

    1-I can't find the position (x,y) of the green circle after move

    Yes, what you've wrote can only record the initialized position, but not the updated one. see the remark section of LeftProperty property, I suggest you could use point.x and point.y to instead.

    'Dim x As Double = CDbl(greencircle.GetValue(Canvas.LeftProperty))

    Dim x As Double = point.X 'Dim y As Double = CDbl(greencircle.GetValue(Canvas.TopProperty))

    Dim y As Double = point.Y


    2- how can I now if the green circle in side the red one?

    Calculate the position is fine, there is no API for hit test, for instance you can calculate if the border of the green circle include in the red circle.

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    • Edited by Jamles HezModerator Monday, December 16, 2013 2:38 PM
    • Marked as answer by imr2k1 Tuesday, December 17, 2013 3:02 AM
    Monday, December 16, 2013 11:25 AM
    Moderator
  • thanks it worked fine.

    for part 2 i found this : point inside circle

    Tuesday, December 17, 2013 3:03 AM

All replies

  • Hi imr2k1,

    1-I can't find the position (x,y) of the green circle after move

    Yes, what you've wrote can only record the initialized position, but not the updated one. see the remark section of LeftProperty property, I suggest you could use point.x and point.y to instead.

    'Dim x As Double = CDbl(greencircle.GetValue(Canvas.LeftProperty))

    Dim x As Double = point.X 'Dim y As Double = CDbl(greencircle.GetValue(Canvas.TopProperty))

    Dim y As Double = point.Y


    2- how can I now if the green circle in side the red one?

    Calculate the position is fine, there is no API for hit test, for instance you can calculate if the border of the green circle include in the red circle.

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    • Edited by Jamles HezModerator Monday, December 16, 2013 2:38 PM
    • Marked as answer by imr2k1 Tuesday, December 17, 2013 3:02 AM
    Monday, December 16, 2013 11:25 AM
    Moderator
  • thanks it worked fine.

    for part 2 i found this : point inside circle

    Tuesday, December 17, 2013 3:03 AM