Page 1 of 1

Find the position of points

Posted: Mon Jul 26, 2021 11:56 am
by [blendman]
Hi

Do you know how to calcul the "tangent" point in that kind of figure ?
Image

When I draw, I get the Black curve, with the points in orange (A, C, E). I konw the position X/Y of this points.

What I would like is to change those points and get the position of : B, C, D . (C has the same position as previously of course, only and E have to change°
A should take the position of B
E should get the position of D
To get the curve in grey.

My drawing is made with the lib vector drawing.
I can post a code if needed.

Do you know how I can calculate that ?

Thank you a lot.

EDIT :

Code: Select all

; Made with Cartoon ! 0.30
; Date : 2021/07/26, 14:10:41

Global Zoom.f =2
Structure sPoint
  x.d
  y.d
EndStructure
Global Dim node.spoint(6)
line$ ="103,295,102,197,103,180,173,120,252,170,253,196,256,295"
For i=0 To 6
  Node(i)\x = Val(StringField(line$,i*2+1,","))
  Node(i)\Y = Val(StringField(line$,i*2+2,","))
Next 

Procedure Object0(x,y)
  
  Protected Z.f
  Z = Zoom
  ; draw the curve
  MovePathCursor((node(0)\x+x)*Z, (node(0)\y+y)*Z)
  For i=1 To ArraySize(node())-2 Step 3
    AddPathCurve( (node(i)\x+x)*z, (node(i)\y+y)*z, (node(i+1)\x+x)*z, (node(i+1)\y+y)*z, (node(i+2)\x+x)*z, (node(i+2)\y+y)*z)
  Next
  VectorSourceColor(RGBA(0,0,0,255))
  StrokePath(3,#PB_Path_RoundEnd)
  
  ; the circle to see the point
  For i= 0 To ArraySize(node())-1
    If Mod(i,3)=0 Or Mod(i,3) = 2
    x1.d = (node(i+1)\x+x)*z
    y1.d = (node(i+1)\y+y)*z
    MovePathCursor((node(i)\x+x)*Z, (node(i)\y+y)*Z)
    AddPathLine(x1,y1)
    VectorSourceColor(RGBA(0,0,0,100))
    DashPath(2,5)
    EndIf
  Next
  
  MovePathCursor((node(0)\x+x)*Z, (node(0)\y+y)*Z)
  For i= 0 To ArraySize(node())
    x1.d = (node(i)\x+x)*z
    y1.d = (node(i)\y+y)*z
    AddPathCircle(x1,y1,10)
    VectorSourceColor(RGBA(255,0,0,100))
    StrokePath(2)
    If Mod(i,3)=0
      AddPathCircle(x1,y1,7)
      VectorSourceColor(RGBA(0,255,255,100))
      FillPath()
    EndIf
  Next
  
EndProcedure 


; code test
w = 1024
h = 600
OpenWindow(0, 0, 0, w, h, "Code Test", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
CanvasGadget(0,0,0,w,h)
If StartVectorDrawing(CanvasVectorOutput(0))
  AddPathBox(0,0, GadgetWidth(0),GadgetHeight(0))
  VectorSourceColor(RGBA(120,120,120,255))
  FillPath()
  Object0(0,-50)
  StopVectorDrawing()
EndIf

Repeat
  Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow

Re: Find the position of points

Posted: Mon Jul 26, 2021 8:30 pm
by marc_256
Hi blendman,

Do you just want to know the angle between lines C,D - C,E

You can use Atan2 distance C,D and C,E

DeltaX = Dx-Cx
DeltaY = Ey-Dy
Angle = Atan2(DeltaY, DeltaX)
for this you need one 90deg corner.

If Dx <> Ex then you can use the CosineRule
then you don't have the good triangle...

Greetings,
Marc

Re: Find the position of points

Posted: Mon Jul 26, 2021 8:55 pm
by STARGÅTE
It would be helpful to know the requirements for the Points B and D.
  • It looks like B, C, and D should lie on one line?
  • It looks like the distance BC = CD?
  • What about the direction B->C regarding A->E ?
  • What about the distance between B and D regarding A and E?
  • In conclusion, what conditions must be fulfilled for the gray curve?

Re: Find the position of points

Posted: Mon Jul 26, 2021 8:55 pm
by marc_256
If I understand well,

DATA:
What you know =
Point C = Cx, Cy
Ax = MouseX
Ay = MouseY

So,
Bx = Ax
By = Cy

DeltaX = Abs (Cx - Bx)
DeltaY = Abs (Cy - Ay)
Angle = Atan2(DeltaX, DeltaY)

Example:
Cx = 200
Cy = 100
MouseX = Ax = 150
MouseY = Ay = 200

Bx = Ax = 150
By = Cy = 100

DeltaX = abs (200 - 150) = 50
DeltaY = abs (200 - 100) = 100

atan2 (50, 100) =
1.1071487177940904 RAD
63.43494882292201 DEG

Marc,

Re: Find the position of points

Posted: Tue Jul 27, 2021 10:58 am
by [blendman]
Hi

Thank you very much with your answers and help :)

STARGÅTE wrote: Mon Jul 26, 2021 8:55 pm It would be helpful to know the requirements for the Points B and D
  • It looks like B, C, and D should lie on one line?
    ---> yes, it seems B, C, D should be on one line (which should be the tangent in C)
  • It looks like the distance BC = CD?
    ---> Not obligatory. Distance BC can be <> CD
  • What about the direction B->C regarding A->E ?
    ---> the direction can be very differents (see the images in this message ;)), The points A, C, E are created when I draw on the canvas
  • What about the distance between B and D regarding A and E?
    ---> can be = or <> too. not obligatory the same.
  • In conclusion, what conditions must be fulfilled for the gray curve?
    ---> the points B,C, D should be on a line, and the are the tangent in C and the curve is smoothed thanks to those points
like this :

The curve I can have before transformation :
Image

What I would like to have after transformation (with a procedure, not made by hand ^^) :
Image


Before :
Image
After :
Image



But in general, I will have this kind of curve (before) :
Image

And I would like that kind of result, after transformation :
Image


marc_256 wrote: Mon Jul 26, 2021 8:55 pm If I understand well,

DATA:
What you know =
Point C = Cx, Cy
Ax = MouseX
Ay = MouseY

So,
Bx = Ax
By = Cy
In fact, it seems to ok with the first image I have posted, but the points A, C, E aren't always vertically, they can be in any direction, like on my previous images ;).

Thank you a lot.


Here is the code for my tests :
http://www.dracaena-studio.com/cartoona ... rveline.pb

Re: Find the position of points

Posted: Tue Jul 27, 2021 11:24 am
by RASHAD

Re: Find the position of points

Posted: Tue Jul 27, 2021 12:04 pm
by [blendman]
Hi

Thank you Rashad.
Unfortunately, it doesn't help me ^^.

For the first code, I have the same functions in my tool.

What I try to do is :
- draw in real time with mouse or tablet wacom (like in Inkscape with pen)
- simplify the curve
- apply a kind of transformation to avoid the "hard" corners made when drawing.
- and get a beautiful smoothed curve as result :D .

Re: Find the position of points

Posted: Tue Jul 27, 2021 12:34 pm
by STARGÅTE
[blendman] wrote: Tue Jul 27, 2021 10:58 am
  • It looks like B, C, and D should lie on one line?
    ---> yes, it seems B, C, D should be on one line (which should be the tangent in C)
  • It looks like the distance BC = CD?
    ---> Not obligatory. Distance BC can be <> CD
  • What about the direction B->C regarding A->E ?
    ---> the direction can be very differents (see the images in this message ;)), The points A, C, E are created when I draw on the canvas
  • What about the distance between B and D regarding A and E?
    ---> can be = or <> too. not obligatory the same.
  • In conclusion, what conditions must be fulfilled for the gray curve?
    ---> the points B,C, D should be on a line, and the are the tangent in C and the curve is smoothed thanks to those points
Your information are still not enough to calculate a clear result.

For axample, based on this initial state:
Image

All the following images meet your current conditions: tangent in the top point, distances left and right to the point need not to be equal.
ImageImageImage

How to decide, which results meet your definition of "smooth"?

Re: Find the position of points

Posted: Tue Jul 27, 2021 12:57 pm
by [blendman]
STARGÅTE wrote: Tue Jul 27, 2021 12:34 pm Your information are still not enough to calculate a clear result.
(...)
How to decide, which results meet your definition of "smooth"?

hi, thanks ;)

the pathround2.PNG seems good.
The pathround4.PNG will not work I guess.

Perhaps it could be interesting to have a kind of "factor" of smoothing ;
0 : we have the figure pathround1.PNG
1.0 : we have the figure pathround2.PNG

Between 0 and 1.0, we have a figure not smoothed as the pathround2.png but more than the pathround1.PNG.

Re: Find the position of points

Posted: Tue Jul 27, 2021 1:13 pm
by STARGÅTE
[blendman] wrote: Tue Jul 27, 2021 12:57 pm the pathround2.PNG seems good.
The pathround4.PNG will not work I guess.
And why the first picture of the second line "seems good"?
What about these images, where a anchors of the bottom left point is longer:
ImageImageImage
The third picture looks not very "smooth", but uses the same calculation than the first picture.

So, for me it is still not obvious what conditions are required for the anchors of the top point.
But you have to define such conditions, otherwise we cannot give you a calculation formula based on x,y of the other points/anchors.

Re: Find the position of points

Posted: Tue Jul 27, 2021 5:17 pm
by [blendman]
STARGÅTE wrote: Tue Jul 27, 2021 1:13 pm And why the first picture of the second line "seems good"?
I think it seems good because there not too much difference in distance between all points.


But, I should try with several drawings to see if this transformation (calcul) works for most of case :).

Re: Find the position of points

Posted: Wed Jul 28, 2021 9:48 am
by [blendman]
Hi

I have seen the example PAthPointX()/pathpointY() and pathpointAngle().

Maybe we can use pathpointAngle() to find the angle in C, and do the transformation with the angle that we get in C :

Code: Select all

AngleInC = PathpointAngle(pathlength())
newangle = radian(90 - angleInC)
DistanceForD = Sqr(Pow(Cx-Ex, 2) + Pow(Cy-Ey, 2)) 
DistanceForB = Sqr(Pow(Cx-Ax, 2) + Pow(Cy-Ay, 2))

; And I place the new point D and B :
DX = cx+ sin(newangle) * distanceForD
Dy = cy +cos(newangle) * distanceForD
Bx = cx - sin(newangle) * distanceForB
By = cy - cos(newangle) * distanceForB