Message Boards Message Boards

0
|
1900 Views
|
3 Replies
|
2 Total Likes
View groups...
Share
Share this post:

How to optimize the program for calculating the distance between points and straight lines?

Posted 10 months ago

How to optimize the calculation from point to line?

Clear["Global`*"]

line = a x + b y + c == 0

pt = {x0, y0}

pol = Apply[Subtract, line]

Flatten@CoefficientList[pol, {x, y}]

A1 = Coefficient[Apply[Subtract, line], x]

B1 = Coefficient[Apply[Subtract, line], y]

C1 = Select[Apply[Subtract, line], FreeQ[x | y]]

distance = Abs[A1 pt[[1]] + B1 pt[[2]] + C1]/Sqrt[A1^2 + B1^2]
POSTED BY: Lee Tao
3 Replies

Here is a way to shorten it a little:

line = a x + b y + c == 0;
pt = {x0, y0};
pol = Apply[Subtract, line];
A1 = Coefficient[pol, x]
B1 = Coefficient[pol, y]
distance = Abs[pol /. Thread[{x, y} -> pt]]/Sqrt[A1^2 + B1^2]
POSTED BY: Gianluca Gorni

Optimize for what purpose? Numerical calculation?

POSTED BY: Gianluca Gorni
Posted 10 months ago

It's just to see if my code can be optimized or written more succinctly. Or what other good methods are there.

POSTED BY: Lee Tao
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract