2
$\begingroup$

In the process of modelling the interaction of charged particles on circle I encountered s.c. circular values calculations.
Consider unit circle and set some point $O$ as starting.
The position of any point is defined by parameter $0 \leq t < 1$ so $t(O) = 0$.
The simplest question: what is the shortest distance (on circle in parameter sense)
between points $t_1$ and $t_2$? The answer is not complex, but not trivial:
$$\min(|t_1-t_2|, 1-|t_1-t_2|)$$
Mathematica:

circDist = Compile[{{t1, _Real}, {t2, _Real}},
   Min[{#, 1 - #} &@Abs[t1 - t2]]];

But a little more complicated question: how to find the middle point of the shortest interval between points
caused some difficulties.
Based on this article, I wrote the function:

circMid[t1_, t2_] :=
  With[{\[Phi]1 = 2 \[Pi] t1, \[Phi]2 = 2 \[Pi] t2},
  Mod[ .5 ArcTan[Cos@\[Phi]1 + Cos@\[Phi]2,
    Sin@\[Phi]1 + Sin@\[Phi]2]/\[Pi],1]];

It gives the right results. But for my purposes I need a very fast, compact function, only with conditions and arithmetic operations.
Looking at the contour plot, it seems that one can write a not too complicated Piecewise function:

enter image description here

I would appreciate your help!
Generally there is very little information about circular values calculations. I found only this library in C++.
Maybe we should write a package for Mathematica. Especially since, for example, circular time calculations in Mathematica seem to work incorrectly.
For example:

TimeObject[{1}] - TimeObject[{23}]
Out: Quantity[-22., "Hours"]

Mean[{TimeObject[{22}], TimeObject[{1}]}]
Out: TimeObject[{11, 30, 0}]
$\endgroup$
0

3 Answers 3

3
$\begingroup$

To long for a comment:

Small modification of @CraigCarter's nice answer

midpoint[{t1_, t2_}] := 
 With[{dist = Abs[t1 - t2]}, 
  Which[dist <= 1 - dist, {dist, Mean[{t1, t2}]}, 
   True, {1 - dist, Mod[  Mean[{t1, t2}] + 1/2  , 1]  }]]

   Manipulate[ 
 Graphics[{ Circle[], Point[{e[t1], e[t2]}], Red, 
   Point[e[tm = Last@midpoint[{t1, t2}]]]}, 
  PlotLabel -> " tm=" <> ToString[tm]], {{t1, 0.9}, 0, 1, 
  Appearance -> "Labeled"}, {{t2, 0.1}, 0, 1, 
  Appearance -> "Labeled"}]

enter image description here

$\endgroup$
2
  • $\begingroup$ Thanks. Seeing (visualizing) is believing… $\endgroup$ Commented Jun 9, 2025 at 9:10
  • $\begingroup$ Aha, thank you that works! $\endgroup$ Commented Jun 9, 2025 at 9:12
4
$\begingroup$

I'm not sure if this is what you are looking for:

midpoint[{t1_, t2_}] := With[{dist = Abs[t1 - t2]},
  Which[dist <= 1 - dist, {dist, Mean[{t1, t2}]}, 
   True, {1 - dist, Mean[{t1, 1 - t2}]}
   ]
  ]

This returns your distance and (what I think you mean by the midpoint, example:

midpoint[{1, .9}] (*{0.1, 0.95}*)

And has the possiblr advantage of not having to recompute dist.

It should be compile-able. The Which (or If) shouldn't be much more expensive than your Min.

$\endgroup$
3
  • $\begingroup$ Note: Compile optimizes code, so this circDist = Compile[{{t1, _Real}, {t2, _Real}}, With[{d = Abs[t1 - t2]}, Min[{d, 1 - d}]]] is a little faster. $\endgroup$ Commented Jun 9, 2025 at 6:40
  • $\begingroup$ Thank you, but Last@midpoint[{0.9,0.1}] is 0.9, but it's obviously 0 or 1 $\endgroup$ Commented Jun 9, 2025 at 7:32
  • 2
    $\begingroup$ @CraigCarter I think midpoint[{t1_, t2_}] := With[{dist = Abs[t1 - t2]}, Which[dist <= 1 - dist, {dist, Mean[{t1, t2}]}, True, {1 - dist, Mean[{t1, t2}] + 1/2}]] is the correct way. It fullfills Last@midpoint[{0.9,0.1}]==1 as lesobrod mentions. $\endgroup$ Commented Jun 9, 2025 at 8:00
4
$\begingroup$

Modular arithmetics on the unit circle of arclength 1 is the natural method of choice (and binarily superfast)

Manipulate[
 Plot[{Abs@Mod[(x - y), 1, -(1/2)] + 0.1, 
       Min[Abs[x - y], 1 - Abs[x - y]]}, {x, -4, 4}, 
          PlotLegends -> "Expressions", ImageSize -> Small], 
    {{y, 1.5}, -4, 4},  ControlPlacement -> Top]

Distance on a Circle

As you see without the mod function you never get full periodicity in both variables

$\endgroup$
1
  • $\begingroup$ @Roland_F I think the OP’s t is already 0 < t < 1. If not perhaps he could mod to -1 < t < 2 and make three copies: t-1,t,t+1 and then use Min on those. $\endgroup$ Commented Jun 9, 2025 at 7:04

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.