Heart of Art ;-)

So I was playing around with code and ended up having Xojo draw a heart just with code, no premade images. Its not all that useful but rather cool I think, Don’t know if anyway has done this before in Xojo or not.

Anyway just wanted to share this -

3 Likes

nice shape,
i found few pages about formula.
https://www.mathematische-basteleien.de/heart.htm
https://blogs.lcps.org/academiesonline/2021/02/13/the-equation-of-the-heart/

1 Like

Looks complicated

WolframAlpha: polar r=(sin(t)*sqrt(abs(cos(t))))/(sin(t) + 7/5) -2*sin(t) + 2

The thing in here is plotting the graph in Xojo.

Thanks.

Mathematically perhaps but programming wise its not too complex. . I just have a loop and then it uses sin & cos.

Ah cool. Not quite the same as my code.

Heart

my take

I want to see the codes.

Not sure if a cheater or a tremendous talent. :rofl:

Well, isn’t that an interesting little Xojo project for those interested in such a thing…?

Example Solution | Code

Put this in the Paint Event of a Canvas:

Sub Paint(g As Graphics, areas() As Rect) Handles Paint
  #Pragma unused areas
  
  g.DrawingColor = &cff0000
  
  Const pi = 3.14159265358979
  
  Var w As Integer = g.Width
  Var h As Integer = g.Height
  Var centerX As Integer = w / 2
  Var centerY As Integer = h / 2
  
  ' Determine the minimum and maximum X/Y values
  Var xMax As Double = -1000000
  Var xMin As Double = 1000000
  Var yMax As Double = -1000000
  Var yMin As Double = 1000000
  
  ' Loop to determine the boundary values
  For t As Double = 0 To 2 * pi Step 0.01
    Var r As Double = (Sin(t) * Sqrt(Abs(Cos(t)))) / (Sin(t) + 7/5) - 2 * Sin(t) + 2
    
    ' Calculate Cartesian coordinates
    Var x As Double = r * Cos(t)
    Var y As Double = r * Sin(t)
    
    ' Update the boundary values
    If x > xMax Then xMax = x
    If x < xMin Then xMin = x
    If y > yMax Then yMax = y
    If y < yMin Then yMin = y
  Next
  
  ' Determine the scaling based on the maximum distances
  Var rangeX As Double = xMax - xMin
  Var rangeY As Double = yMax - yMin
  Var scaleX As Double = (w - 20) / rangeX
  Var scaleY As Double = (h - 20) / rangeY
  Var scale As Double = Min(scaleX, scaleY)  ' Uniform scaling
  
  ' Calculate the offset to center the image
  Var offsetX As Double = (xMax + xMin) / 2
  Var offsetY As Double = (yMax + yMin) / 2
  
  ' Draw the function with correct scaling and offset
  Var prevX As Double
  Var prevY As Double
  Var first As Boolean = True
  
  For t As Double = 0 To 2 * pi Step 0.01
    Var r As Double = (Sin(t) * Sqrt(Abs(Cos(t)))) / (Sin(t) + 7/5) - 2 * Sin(t) + 2
    
    ' Convert to Cartesian coordinates
    Var x As Double = (r * Cos(t) - offsetX) * scale
    Var y As Double = (r * Sin(t) - offsetY) * scale
    
    ' Convert to canvas coordinates
    x = centerX + x
    y = centerY - y
    
    ' Draw a line between points
    If Not first Then
      g.DrawLine(prevX, prevY, x, y)
    Else
      first = False
    End If
    
    ' Store the current point for the next line
    prevX = x
    prevY = y
  Next
  
End Sub

The forum now accepts that people drag some zip samples into their comments.

Your sample:

heart_jürgo.zip (4.9 KB)

I made ir just for fun, because I could. It’s for sure generated by a Xojo app, but not using a particular formula. Instead, it was generated by a powerful free form 3D modeler (took five or ten minutes to model), including generating a one second movie that generated the motion. The movie was then converted into a moving GIF with the Gifsky utility app, as the forum does not allow movies.

heart_ok

I cheated here. No Xojo involved.

Mine doesn’t use as much code as yours but thats pretty cool. :sunglasses: :grin:

BIG step for new IA!

This way, robotics will be more human-like machines

:face_with_monocle: