Region and Infinite Line Intersection does not yield correct results with Maximize

I have a list of points pixelvaluepos that I can use to convert to a BoundaryMeshRegion. I also happen to know the centroid centerofMass of the region as well as a point pointonAxisofSymmetry that lies very close to the centroid. Now I can form an InfiniteLine using the centroid and the point.

pixelvaluepos = {{405, 501}, {406, 501}, {407, 501}, {408, 501}, {409,
501}, {410, 501}, {411, 501}, {418, 501}, {419, 501}, {420,
501}, {421, 501}, {422, 501}, {423, 501}, {424, 501}, {425,
501}, {426, 501}, {427, 501}, {428, 501}, {429, 501}, {430,
501}, {431, 501}, {432, 501}, {433, 501}, {434, 501}, {435,
501}, {436, 501}, {437, 501}, {438, 501}, {393, 500}, {394,
500}, {395, 500}, {396, 500}, {397, 500}, {398, 500}, {399,
500}, {400, 500}, {401, 500}, {402, 500}, {403, 500}, {404,
500}, {405, 500}, {411, 500}, {412, 500}, {413, 500}, {414,
500}, {415, 500}, {416, 500}, {417, 500}, {418, 500}, {438,
500}, {439, 500}, {440, 500}, {441, 500}, {388, 499}, {389,
499}, {390, 499}, {391, 499}, {392, 499}, {393, 499}, {441,
499}, {442, 499}, {386, 498}, {387, 498}, {388, 498}, {442,
498}, {443, 498}, {385, 497}, {386, 497}, {443, 497}, {444,
497}, {445, 497}, {446, 497}, {382, 496}, {383, 496}, {384,
496}, {385, 496}, {446, 496}, {447, 496}, {448, 496}, {380,
495}, {381, 495}, {382, 495}, {448, 495}, {449, 495}, {450,
495}, {379, 494}, {380, 494}, {450, 494}, {451, 494}, {377,
493}, {378, 493}, {379, 493}, {451, 493}, {452, 493}, {453,
493}, {374, 492}, {375, 492}, {376, 492}, {377, 492}, {453,
492}, {454, 492}, {373, 491}, {374, 491}, {454, 491}, {455,
491}, {456, 491}, {457, 491}, {458, 491}, {370, 490}, {371,
490}, {372, 490}, {373, 490}, {458, 490}, {459, 490}, {369,
489}, {370, 489}, {459, 489}, {460, 489}, {461, 489}, {462,
489}, {367, 488}, {368, 488}, {369, 488}, {462, 488}, {463,
488}, {464, 488}, {366, 487}, {367, 487}, {464, 487}, {465,
487}, {466, 487}, {467, 487}, {362, 486}, {363, 486}, {364,
486}, {365, 486}, {366, 486}, {467, 486}, {468, 486}, {469,
486}, {470, 486}, {360, 485}, {361, 485}, {362, 485}, {470,
485}, {471, 485}, {359, 484}, {360, 484}, {471, 484}, {472,
484}, {473, 484}, {356, 483}, {357, 483}, {358, 483}, {359,
483}, {473, 483}, {474, 483}, {355, 482}, {356, 482}, {474,
482}, {353, 481}, {354, 481}, {355, 481}, {474, 481}, {475,
481}, {352, 480}, {353, 480}, {475, 480}, {476, 480}, {351,
479}, {352, 479}, {476, 479}, {477, 479}, {351, 478}, {477,
478}, {478, 478}, {350, 477}, {351, 477}, {478, 477}, {479,
477}, {349, 476}, {350, 476}, {479, 476}, {348, 475}, {349,
475}, {479, 475}, {480, 475}, {348, 474}, {480, 474}, {481,
474}, {347, 473}, {348, 473}, {481, 473}, {346, 472}, {347,
472}, {481, 472}, {482, 472}, {345, 471}, {346, 471}, {482,
471}, {483, 471}, {345, 470}, {483, 470}, {344, 469}, {345,
469}, {483, 469}, {484, 469}, {343, 468}, {344, 468}, {484,
468}, {343, 467}, {484, 467}, {342, 466}, {343, 466}, {484,
466}, {485, 466}, {341, 465}, {342, 465}, {485, 465}, {486,
465}, {340, 464}, {341, 464}, {486, 464}, {339, 463}, {340,
463}, {486, 463}, {487, 463}, {339, 462}, {487, 462}, {339,
461}, {487, 461}, {488, 461}, {338, 460}, {339, 460}, {488,
460}, {337, 459}, {338, 459}, {488, 459}, {489, 459}, {337,
458}, {489, 458}, {336, 457}, {337, 457}, {489, 457}, {336,
456}, {489, 456}, {336, 455}, {489, 455}, {490, 455}, {335,
454}, {336, 454}, {490, 454}, {335, 453}, {490, 453}, {335,
452}, {490, 452}, {491, 452}, {334, 451}, {335, 451}, {491,
451}, {492, 451}, {334, 450}, {492, 450}, {334, 449}, {492,
449}, {334, 448}, {492, 448}, {334, 447}, {492, 447}, {333,
446}, {334, 446}, {492, 446}, {333, 445}, {492, 445}, {333,
444}, {492, 444}, {333, 443}, {492, 443}, {333, 442}, {492,
442}, {493, 442}, {333, 441}, {493, 441}, {333, 440}, {493,
440}, {333, 439}, {493, 439}, {494, 439}, {333, 438}, {494,
438}, {333, 437}, {494, 437}, {333, 436}, {494, 436}, {333,
435}, {494, 435}, {495, 435}, {333, 434}, {495, 434}, {333,
433}, {495, 433}, {333, 432}, {495, 432}, {333, 431}, {495,
431}, {496, 431}, {333, 430}, {496, 430}, {333, 429}, {496,
429}, {333, 428}, {496, 428}, {333, 427}, {496, 427}, {333,
426}, {496, 426}, {333, 425}, {496, 425}, {333, 424}, {496,
424}, {333, 423}, {496, 423}, {333, 422}, {496, 422}, {333,
421}, {496, 421}, {333, 420}, {496, 420}, {333, 419}, {496,
419}, {333, 418}, {496, 418}, {333, 417}, {496, 417}, {333,
416}, {496, 416}, {333, 415}, {496, 415}, {333, 414}, {496,
414}, {333, 413}, {496, 413}, {333, 412}, {496, 412}, {333,
411}, {334, 411}, {496, 411}, {334, 410}, {496, 410}, {334,
409}, {496, 409}, {334, 408}, {496, 408}, {334, 407}, {496,
407}, {334, 406}, {335, 406}, {496, 406}, {335, 405}, {496,
405}, {335, 404}, {336, 404}, {496, 404}, {336, 403}, {496,
403}, {336, 402}, {495, 402}, {496, 402}, {336, 401}, {495,
401}, {336, 400}, {337, 400}, {494, 400}, {495, 400}, {337,
399}, {494, 399}, {337, 398}, {494, 398}, {337, 397}, {494,
397}, {337, 396}, {338, 396}, {494, 396}, {338, 395}, {339,
395}, {494, 395}, {339, 394}, {494, 394}, {339, 393}, {340,
393}, {494, 393}, {340, 392}, {493, 392}, {494, 392}, {340,
391}, {493, 391}, {340, 390}, {341, 390}, {493, 390}, {341,
389}, {493, 389}, {341, 388}, {342, 388}, {493, 388}, {342,
387}, {493, 387}, {342, 386}, {343, 386}, {493, 386}, {343,
385}, {344, 385}, {492, 385}, {493, 385}, {344, 384}, {492,
384}, {344, 383}, {345, 383}, {491, 383}, {492, 383}, {345,
382}, {491, 382}, {345, 381}, {346, 381}, {491, 381}, {346,
380}, {491, 380}, {346, 379}, {347, 379}, {490, 379}, {491,
379}, {347, 378}, {348, 378}, {490, 378}, {348, 377}, {490,
377}, {348, 376}, {349, 376}, {350, 376}, {490, 376}, {350,
375}, {490, 375}, {350, 374}, {351, 374}, {489, 374}, {490,
374}, {351, 373}, {352, 373}, {489, 373}, {352, 372}, {353,
372}, {489, 372}, {353, 371}, {354, 371}, {488, 371}, {489,
371}, {354, 370}, {355, 370}, {488, 370}, {355, 369}, {356,
369}, {487, 369}, {488, 369}, {356, 368}, {487, 368}, {356,
367}, {486, 367}, {487, 367}, {356, 366}, {357, 366}, {485,
366}, {486, 366}, {357, 365}, {358, 365}, {484, 365}, {485,
365}, {358, 364}, {359, 364}, {483, 364}, {484, 364}, {359,
363}, {360, 363}, {482, 363}, {483, 363}, {360, 362}, {361,
362}, {482, 362}, {361, 361}, {481, 361}, {482, 361}, {361,
360}, {362, 360}, {480, 360}, {481, 360}, {362, 359}, {363,
359}, {480, 359}, {363, 358}, {364, 358}, {479, 358}, {480,
358}, {364, 357}, {365, 357}, {478, 357}, {479, 357}, {365,
356}, {366, 356}, {478, 356}, {366, 355}, {367, 355}, {476,
355}, {477, 355}, {478, 355}, {367, 354}, {475, 354}, {476,
354}, {367, 353}, {368, 353}, {369, 353}, {475, 353}, {369,
352}, {370, 352}, {474, 352}, {475, 352}, {370, 351}, {371,
351}, {473, 351}, {474, 351}, {371, 350}, {372, 350}, {472,
350}, {473, 350}, {372, 349}, {373, 349}, {470, 349}, {471,
349}, {472, 349}, {373, 348}, {374, 348}, {468, 348}, {469,
348}, {470, 348}, {374, 347}, {375, 347}, {376, 347}, {377,
347}, {466, 347}, {467, 347}, {468, 347}, {377, 346}, {378,
346}, {463, 346}, {464, 346}, {465, 346}, {466, 346}, {378,
345}, {379, 345}, {462, 345}, {463, 345}, {379, 344}, {380,
344}, {381, 344}, {460, 344}, {461, 344}, {462, 344}, {381,
343}, {382, 343}, {458, 343}, {459, 343}, {460, 343}, {382,
342}, {383, 342}, {455, 342}, {456, 342}, {457, 342}, {458,
342}, {383, 341}, {384, 341}, {385, 341}, {386, 341}, {453,
341}, {454, 341}, {455, 341}, {386, 340}, {387, 340}, {450,
340}, {451, 340}, {452, 340}, {453, 340}, {387, 339}, {388,
339}, {445, 339}, {446, 339}, {447, 339}, {448, 339}, {449,
339}, {450, 339}, {388, 338}, {389, 338}, {443, 338}, {444,
338}, {445, 338}, {389, 337}, {390, 337}, {391, 337}, {441,
337}, {442, 337}, {443, 337}, {391, 336}, {392, 336}, {435,
336}, {436, 336}, {437, 336}, {438, 336}, {439, 336}, {440,
336}, {441, 336}, {392, 335}, {393, 335}, {394, 335}, {395,
335}, {434, 335}, {435, 335}, {395, 334}, {396, 334}, {432,
334}, {433, 334}, {434, 334}, {396, 333}, {397, 333}, {398,
333}, {427, 333}, {428, 333}, {429, 333}, {430, 333}, {431,
333}, {432, 333}, {398, 332}, {399, 332}, {425, 332}, {426,
332}, {427, 332}, {399, 331}, {400, 331}, {401, 331}, {416,
331}, {417, 331}, {418, 331}, {419, 331}, {420, 331}, {421,
331}, {422, 331}, {423, 331}, {424, 331}, {425, 331}, {401,
330}, {402, 330}, {403, 330}, {404, 330}, {405, 330}, {406,
330}, {407, 330}, {408, 330}, {409, 330}, {410, 330}, {411,
330}, {412, 330}, {413, 330}, {414, 330}, {415, 330}, {416, 330}};


the following line of code can be used to create the region

region1 = BoundaryMeshRegion[pixelvaluepos,Line@Last@FindShortestTour[pixelvaluepos]];


the centerofMass and pointonAxisofSymmetry are given below:

centerofMass = {415.3830972783377, 417.64887004395507};
pointonAxisofSymmetry = {-0.8790370654563042, -0.47675343475843873};


the InfiniteLine forms region2

 region2 =
InfiniteLine[{centerofMass, centerofMass + pointonAxisofSymmetry}];


Now I wish to calculate the Intersection of region1 and region2 using Maximize. I use the euclidean distance between the points that are elements of both region1 and region2

{pt1,pt2}=Part[Maximize[(x - u)^2 + (y - v)^2, {{x, y} \[Element]
region1, {u, v} \[Element] region1, {x, y} \[Element]
region2, {u, v} \[Element] region2}], 2] /. {x -> xval_, y -> yval_, u ->uval_,v -> vval_} :> {{xval, yval}, {uval, vval}}


However, the results clearly are erroneous since I expect the intersecting points - results of Maximize - to be at the boundaries

Show[region1, Graphics[{region2, Blue, PointSize[0.02], Purple, Point@pt1, Yellow, Point@pt2}]]


Please note that for some other values of pixelvaluepos i get correct results and for others incorrect (see below: row 1 column 2, row2 column 7 etc.. )

Can someone Kindly help me to figure this problem.

Note:

for complete answer applied to the problem cases in the question see Ali's answer

Original posting:

We can do this using RegionIntersection if we prep our regions correctly. All we really need are the boundary of that region and a finite segment assured to cover our region. So first we'll calculate a vector to add to our center of mass:

increment =
Max[EuclideanDistance @@@ Tuples[N@pixelvaluepos, 2]]*
Normalize[{-0.8790370654563042, -0.47675343475843873}];


Then find the boundary path around pixelvaluepos:

tours = FindShortestTour@pixelvaluepos;


Then we can set up our regions as such:

boundary1 = Line@pixelvaluepos[[Last@tours]];
boundary2 =
Line[{centerofMass - increment, centerofMass + increment}];


And if we then use RegionIntersection...

In[894]:= RegionIntersection[
boundary1,
boundary2
]

Out[894]= Point[{{346., 380.018}, {489., 457.576}}]


And then confirm that this worked:

Graphics@{boundary1,
InfiniteLine @@ boundary2,
PointSize[.02], Red,
Point[{{346., 380.01834086432893}, {489., 457.5756406069365}}]}


• +1 for the answer. I am going to apply your answer for all the faulty cases and will accept your answer then. cheers ! – Ali Hashmi Mar 12 '17 at 17:00
• i am accepting your answer. Although for one case there were four points that were obtained. you can use trimList posted in my answer to acquire only two points at the end – Ali Hashmi Mar 12 '17 at 18:34
• Great, I'll put in a note at the bottom of the answer that people should see your answer to get the complete result. – b3m2a1 Mar 12 '17 at 18:35
list = (RegionIntersection[\[ScriptCapitalR]2, #]&/@
MeshPrimitives[\[ScriptCapitalR]1, 1] //Cases[#, Point[arg_] :> arg] &);

trimList[ls_] := Module[{p = ls, pairs, firstelem = First@ls, dist, maxdist},
pairs = {firstelem, #} & /@ p;
dist = EuclideanDistance[##] & @@@ pairs;
maxdist = Max@dist;
pairs[[First @@ Position[dist, maxdist]]]
] /; Length@ls > 2;

{pt1, pt2} = trimList[list] /. trimList[arg__] :> arg;


when the code above is applied to some of the incorrect masks: