How to get the shared length between two linestring?

I want to get the shared length between two linestring. How to achieve this using vba?

  • Hi Dharmarajan,

    Unknown said:
    Since vertices of only 2 selected elements are the same

    It's the most simple situation, because you have to compare vertices directly and can ignore line directions.

    I recommend to don't compare coordinates directly as floating numbers equality is well known math problem. Consequently don't use Point2dEqual (or Point3dEqual) method. In my opinion it's better to use Point2dEqualTolerance (or Point3dEqualTolerance) method and to define a tolerance acceptable for your intention.

    With regards,

      Jan

  • Thanks Jan. Since vertices of only 2 selected elements are the same, I accomplish it by algorithm
  • Hi,

    Unknown said:
    I attached a simple screenshot for your reference

    Well, it's a bit more clear now, but still some important information is missing, at least:

    • Are you looking for a general solution (find all shared parts for all line elements in a model) or for a specific case (only 2 selected elements will be checked)?
    • Is your picture a standard situation, so vertices of different elements are the same, or a situation can be more general and one element can end in a middle of a seconed one (so the vertex lies on element, not on another vertex)?

    Unknown said:
    By using object.GetRegionDifference method, it is possible to get the shared area between two shape element.

    I don't know, try it, it's not very difficult task. I guess it will not work, because region difference relates to closed elements, not linear. But as I wrote, it has to be tested.

    Unknown said:
    Similarly, Is there any such inbuilt function is available for Linestring(apart from writing a alogrithm)?

    I don't know about any such functionality. But if vertices of lines are the same, the alghoritm is not very complex.

    With regards,

      Jan

  • Hi Artur,

    I attached a simple screenshot for your reference, in which the blue and yellow colored elements represents two individual line elements and the Magenta color represents the shared part/length between them.

    Hi Jan,

    By using object.GetRegionDifference method, it is possible to get the shared area between two shape element. Similarly, Is there any such inbuilt function is available for Linestring(apart from writing a alogrithm)?

  • Hi Dharmarajan,

    I agree with Artur is not quite clear what is your requirement and under what conditions.

    Based on some preliminary assumptions in my opinion your question is not the right one. If you define "shared length" as a part, which is the same for two separate lines (the lines overlap), the basic question is about finding a proper algorithm, which does not depend on any particular development language in any way. After the algorithm is found / investigated, it's time to make the second step and try to implement it using chosen language. The language adds own extra constraints, so not all languages are efficient to implement all algorithm.

    A complexity of the algorithm can vary a lot depending on your requirements and conditions: To solve the problem for two already selected lines is completely different comparing to general approach "test all elements, find their line segmenets and test them all".

    For the most simple case, when two lines (not line strings) are already selected, there are some off-hand ideas what to test:

    • Line ranges: If they don't overlap, not reason to test them further.
    • Line vectors: If they are not the same, the can intersect but not overlap. You have to take care about negative direction, because A->B and B->A has to be evaluated as the same, but in a plain vector algebra they are different vectors.
    • If the lines are in the same area and have the same direction, test if start/end point of one line lies on a vector of the second line. If yes, evaluate all possible cases, I think there are only several exist.

    I guess there are plenty of ways how to optimize the process, but there is the rule to implement what works at first and optimize after the functionality is tested.

    With regards,

     Jan

  • Hi Dharmarajan,
    I am unsure if I completely understand your question. Could you please describe this more in detail.
    If possible post a small dgn file as example.
    Thanks
    Artur