{"id":35034,"date":"2024-11-01T09:34:51","date_gmt":"2024-11-01T09:34:51","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=35034"},"modified":"2024-11-01T11:45:14","modified_gmt":"2024-11-01T11:45:14","slug":"kotlin-coding-test-course-determining-the-intersection-of-line-segments","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/35034\/","title":{"rendered":"kotlin coding test course, determining the intersection of line segments"},"content":{"rendered":"<p>Problems regarding the intersection of line segments are frequently presented in programming competitions and coding tests. In this course, we will learn how to determine whether line segments intersect using the Kotlin language. This course will help you develop geometric techniques and mathematical thinking. Additionally, we will validate the accuracy of the algorithm through various test cases.<\/p>\n<h2>Problem Description<\/h2>\n<p>The problem is to determine whether two given line segments intersect. Line segment A is defined by points P1(x1, y1) and P2(x2, y2), and line segment B is defined by points Q1(x3, y3) and Q2(x4, y4). If they intersect, output &#8220;YES&#8221;, and if they do not, output &#8220;NO&#8221;.<\/p>\n<h3>Input Format<\/h3>\n<ul>\n<li>The first line contains four integers x1, y1, x2, y2 separated by spaces, representing the two points P1 and P2 of line segment A.<\/li>\n<li>The second line contains four integers x3, y3, x4, y4 separated by spaces, representing the two points Q1 and Q2 of line segment B.<\/li>\n<\/ul>\n<h3>Output Format<\/h3>\n<p>Output &#8220;YES&#8221; or &#8220;NO&#8221; depending on whether they intersect.<\/p>\n<h2>Approach to the Problem<\/h2>\n<p>To determine the intersection of the line segments, we follow these steps:<\/p>\n<ol>\n<li>We must consider the directionality of each line segment for them to intersect.<\/li>\n<li>We use the endpoints of each segment and the two points of the other segment to determine the direction.<\/li>\n<li>The intersection of the segments can be judged through <b>intervals<\/b> and <b>positions<\/b>.<\/li>\n<\/ol>\n<p>Specifically, we can determine whether points P1 and P2 of segment A are oriented counterclockwise or clockwise with respect to points Q1 and Q2 of segment B. This can be used to discern their intersection.<\/p>\n<h2>Kotlin Code Implementation<\/h2>\n<p>Now let&#8217;s look at the code implemented in Kotlin. The code below is a simple example to determine whether line segments A and B intersect.<\/p>\n<pre>\n<code>\nfun main() {\n    val (x1, y1, x2, y2) = readLine()!!.split(\" \").map { it.toInt() }\n    val (x3, y3, x4, y4) = readLine()!!.split(\" \").map { it.toInt() }\n\n    if (doIntersect(x1, y1, x2, y2, x3, y3, x4, y4)) {\n        println(\"YES\")\n    } else {\n        println(\"NO\")\n    }\n}\n\n\/\/ Calculate the direction of two points\nfun orientation(px: Int, py: Int, qx: Int, qy: Int, rx: Int, ry: Int): Int {\n    val value = (qy - py) * (rx - qx) - (qx - px) * (ry - qy)\n    return when {\n        value == 0 -> 0 \/\/ Collinear\n        value > 0 -> 1  \/\/ Counterclockwise\n        else -> 2       \/\/ Clockwise\n    }\n}\n\n\/\/ Function to determine if two segments intersect\nfun doIntersect(x1: Int, y1: Int, x2: Int, y2: Int, x3: Int, y3: Int, x4: Int, y4: Int): Boolean {\n    val o1 = orientation(x1, y1, x2, y2, x3, y3)\n    val o2 = orientation(x1, y1, x2, y2, x4, y4)\n    val o3 = orientation(x3, y3, x4, y4, x1, y1)\n    val o4 = orientation(x3, y3, x4, y4, x2, y2)\n\n    \/\/ Standard case\n    if (o1 != o2 && o3 != o4) return true\n\n    \/\/ Exception handling for when segments are on the same line\n    if (o1 == 0 && onSegment(x1, y1, x3, y3, x2, y2)) return true\n    if (o2 == 0 && onSegment(x1, y1, x4, y4, x2, y2)) return true\n    if (o3 == 0 && onSegment(x3, y3, x1, y1, x4, y4)) return true\n    if (o4 == 0 && onSegment(x3, y3, x2, y2, x4, y4)) return true\n\n    return false\n}\n\n\/\/ Function to determine if a given point lies on the segment\nfun onSegment(px: Int, py: Int, qx: Int, qy: Int, rx: Int, ry: Int): Boolean {\n    return (rx <= maxOf(px, qx) &#038;&#038; rx >= minOf(px, qx) &&\n            ry <= maxOf(py, qy) &#038;&#038; ry >= minOf(py, qy))\n}\n<\/code>\n<\/pre>\n<h2>Code Explanation<\/h2>\n<p>The code above implements the process of determining the intersection of line segments A and B.<\/p>\n<ul>\n<li><strong>orientation function:<\/strong> This function calculates the orientation of the given three points (P, Q, R). Based on the orientation, the values are classified as 0 (collinear), 1 (counterclockwise), and 2 (clockwise). This value is used to determine the intersection.<\/li>\n<li><strong>doIntersect function:<\/strong> This function calculates the directionality of each segment and evaluates the standard and exceptional cases to return the result.<\/li>\n<li><strong>onSegment function:<\/strong> This function determines whether point R lies on segment PQ by comparing R&#8217;s position with the endpoints of the segment.<\/li>\n<\/ul>\n<h2>Test Cases<\/h2>\n<p>Now we will verify the accuracy of the code through various test cases.<\/p>\n<ul>\n<li>Test Case 1: <code>0 0 2 2<\/code> <code>0 2 2 0<\/code> \u2192 <b>Result:<\/b> YES<\/li>\n<li>Test Case 2: <code>1 1 10 1<\/code> <code>1 2 10 2<\/code> \u2192 <b>Result:<\/b> NO<\/li>\n<li>Test Case 3: <code>10 0 0 10<\/code> <code>0 0 10 10<\/code> \u2192 <b>Result:<\/b> YES<\/li>\n<li>Test Case 4: <code>1 1 3 3<\/code> <code>1 3 3 1<\/code> \u2192 <b>Result:<\/b> YES<\/li>\n<li>Test Case 5: <code>0 0 0 10<\/code> <code>0 5 10 5<\/code> \u2192 <b>Result:<\/b> NO<\/li>\n<\/ul>\n<h2>Conclusion<\/h2>\n<p>In this course, we implemented an efficient algorithm to determine the intersection of line segments using Kotlin. This problem, based on geometric concepts, can be effectively utilized in various situations. Gaining experience in solving diverse problems through applicable algorithms and techniques is important.<\/p>\n<p>It is crucial to implement and execute the code and test cases to clearly understand the functioning of the algorithm. Continuous practice will enhance your problem-solving abilities in geometry.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problems regarding the intersection of line segments are frequently presented in programming competitions and coding tests. In this course, we will learn how to determine whether line segments intersect using the Kotlin language. This course will help you develop geometric techniques and mathematical thinking. Additionally, we will validate the accuracy of the algorithm through various &hellip; <a href=\"https:\/\/atmokpo.com\/w\/35034\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;kotlin coding test course, determining the intersection of line segments&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[106],"tags":[],"class_list":["post-35034","post","type-post","status-publish","format-standard","hentry","category----en"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>kotlin coding test course, determining the intersection of line segments - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/atmokpo.com\/w\/35034\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"kotlin coding test course, determining the intersection of line segments - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Problems regarding the intersection of line segments are frequently presented in programming competitions and coding tests. In this course, we will learn how to determine whether line segments intersect using the Kotlin language. This course will help you develop geometric techniques and mathematical thinking. Additionally, we will validate the accuracy of the algorithm through various &hellip; \ub354 \ubcf4\uae30 &quot;kotlin coding test course, determining the intersection of line segments&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/35034\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:34:51+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:45:14+00:00\" \/>\n<meta name=\"author\" content=\"root\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:site\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:label1\" content=\"\uae00\uc4f4\uc774\" \/>\n\t<meta name=\"twitter:data1\" content=\"root\" \/>\n\t<meta name=\"twitter:label2\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data2\" content=\"4\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/35034\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/35034\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"kotlin coding test course, determining the intersection of line segments\",\"datePublished\":\"2024-11-01T09:34:51+00:00\",\"dateModified\":\"2024-11-01T11:45:14+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/35034\/\"},\"wordCount\":512,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Kotlin coding test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/35034\/\",\"url\":\"https:\/\/atmokpo.com\/w\/35034\/\",\"name\":\"kotlin coding test course, determining the intersection of line segments - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:34:51+00:00\",\"dateModified\":\"2024-11-01T11:45:14+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/35034\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/35034\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/35034\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"kotlin coding test course, determining the intersection of line segments\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/atmokpo.com\/w\/#website\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/atmokpo.com\/w\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"contentUrl\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"width\":400,\"height\":400,\"caption\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\"},\"image\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/bebubo4\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\",\"name\":\"root\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"caption\":\"root\"},\"sameAs\":[\"http:\/\/atmokpo.com\/w\"],\"url\":\"https:\/\/atmokpo.com\/w\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"kotlin coding test course, determining the intersection of line segments - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/atmokpo.com\/w\/35034\/","og_locale":"ko_KR","og_type":"article","og_title":"kotlin coding test course, determining the intersection of line segments - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Problems regarding the intersection of line segments are frequently presented in programming competitions and coding tests. In this course, we will learn how to determine whether line segments intersect using the Kotlin language. This course will help you develop geometric techniques and mathematical thinking. Additionally, we will validate the accuracy of the algorithm through various &hellip; \ub354 \ubcf4\uae30 \"kotlin coding test course, determining the intersection of line segments\"","og_url":"https:\/\/atmokpo.com\/w\/35034\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:34:51+00:00","article_modified_time":"2024-11-01T11:45:14+00:00","author":"root","twitter_card":"summary_large_image","twitter_creator":"@bebubo4","twitter_site":"@bebubo4","twitter_misc":{"\uae00\uc4f4\uc774":"root","\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"4\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/35034\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/35034\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"kotlin coding test course, determining the intersection of line segments","datePublished":"2024-11-01T09:34:51+00:00","dateModified":"2024-11-01T11:45:14+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/35034\/"},"wordCount":512,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Kotlin coding test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/35034\/","url":"https:\/\/atmokpo.com\/w\/35034\/","name":"kotlin coding test course, determining the intersection of line segments - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:34:51+00:00","dateModified":"2024-11-01T11:45:14+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/35034\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/35034\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/35034\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"kotlin coding test course, determining the intersection of line segments"}]},{"@type":"WebSite","@id":"https:\/\/atmokpo.com\/w\/#website","url":"https:\/\/atmokpo.com\/w\/","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","description":"","publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/atmokpo.com\/w\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/atmokpo.com\/w\/#organization","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","url":"https:\/\/atmokpo.com\/w\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/","url":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","contentUrl":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","width":400,"height":400,"caption":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8"},"image":{"@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/bebubo4"]},{"@type":"Person","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7","name":"root","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","caption":"root"},"sameAs":["http:\/\/atmokpo.com\/w"],"url":"https:\/\/atmokpo.com\/w\/author\/root\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/35034","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/comments?post=35034"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/35034\/revisions"}],"predecessor-version":[{"id":35035,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/35034\/revisions\/35035"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=35034"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=35034"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=35034"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}